Dictionary in Pandas Series in Python by R4R Team

Series :
-series is one dimensional array defined in pandas library that can be used to store any data type.

Syntax-

pandas.Series(data,index)

Here data can be
1. scalar value like string,integer,float
2. python dictionary
3. Ndarray

-Here index part is optional, by default it is 0,1,2,3,4,......

Dictionary in Series()

program-

import pandas

#given dictionary
d={1:'one',2:'two',3:'three'}

#with default index value
ans=pandas.Series(d)
print(ans)

#with index value
ans=pandas.Series(d,index=[4,1,5,2])
print(ans)


output-

1 one
2 two
3three
dtype: object
4NaN
1one
5NaN
2two
dtype: object

-In this program, we have a dictionary d={1:'one',2:'two',3:'three'}, then we pass that dictionary in Series() function then output is shown in above output.
-Similarly when we pass that dictionary in Series() function with index value, then match value are shows but when index value is not match with dictionary key then it will shown NaN.




Leave a Comment: