Home > Notes > Python Pandas

pandas Missing Data

pandas Missing Data

Missing Data

Using fillna and dropna we can handling missing data.

# using dropna we can drop the data whcih will have nan
 import numpy as np
 import pandas as pd
 df = pd.DataFrame({'A':[1,2,np.nan],
 'B':[5,np.nan,np.nan],
 'C':[1,2,3]})
 df.dropna() # which will drop The nan rows
 df.dropna(axis=1) # which will drop the nan columns
 df.dropna(thresh=2)

 # using fillna we can fill the replace the nan on given value in fi
 import numpy as np
 import pandas as pd
 df = pd.DataFrame({'A':[1,2,np.nan],
 'B':[5,np.nan,np.nan],
 'C':[1,2,3]})
 df.fillna(value='FILL VALUE')
 df['A'].fillna(value=df['A'].mean())