We will provide an example of how you can impute missing values in Pandas following the rules below:
- If the variable is numeric then impute the missing values with the mean
- If the variable is
object
dtype
then impute the missing values with the mode
import pandas as pd import numpy as np df = pd.DataFrame({'id':list(range(10)), 'A':[10,9,8,7,np.nan,np.nan,20,15,12,np.nan], 'B':["A","B","A","A",np.nan,"B","A","B",np.nan,"A"], 'C':[np.nan,"BB","CC","BB","BB","CC","AA","BB",np.nan,"AA"], 'D':[np.nan,20,18,22,18,17,19,np.nan,17,23]}) df
for i in df.columns: if (df[i].dtype=='object'): df[i].fillna(df[i].mode()[0], inplace=True) elif (df[i].dtype=='float64' or df[i].dtype=='int64'): df[i].fillna(df[i].mean(), inplace=True) df