Tuesday 16 February 2021

1.2 Series in Pandas

 1.2 Series in Pandas


Adding NaN values in a Series:

You can specify missing or empty value using np.NaN or None

 

S = pd.Series([6.5,np.NaN,2.34])

 

import pandas as pd

import numpy as np

S=pd.Series([6.5,np.NaN, 2.34])

print(S)

 

S=pd.Series([6.5,None, 2.34])

print(S)




Specify Index in Series:

S = pd.Series(data=data_values, index=index_values)

 

1.       Create a Series with month name as index and no. of days in month as data of series.

 

import pandas as pd

I=['Jan','Feb','Mar','Apr']

D=[31,28,31,30]

S=pd.Series(D,I)                                               #by default first argument is data and 2nd is Index

print(S)

 

S=pd.Series(I,D)                                               #by default first argument is data and 2nd is Index

print(S)

 

S=pd.Series(index=I,data=D)

print(S)

 

S=pd.Series(data=D,index=I)

print(S)

 




1.       A List namely section that store the section names of class 12 and another list contri store the contribution made by students section wise. Create a Series for these data.

 

import pandas as pd

section=['A','B','C','D']

contri=[6700,5600,5000,5200]

S=pd.Series(data=contri,index=section)

print(S)



Specify data type in Series

S = pd.Series(data=None, index=None, dtype=None)

import pandas as pd

import numpy as np

months=['Jan','Feb','Mar','Apr']

days=[31,28,31,30]

S=pd.Series(data=days,index=months,dtype=np.float64)

print(S)




 

No comments:

Post a Comment