Skip to content Skip to sidebar Skip to footer

Replace Missing Values At Once In Both Categorical And Numerical Columns

Is there a way to replace NAN values in both categorical columns as well as numerical columns at once? A very simplistic example: data = {'col_1': [3, np.nan, 1, 2], 'col_2': ['a'

Solution 1:

mean will only work for numeric types, so fill that first then fill the remainder with mode.

df.fillna(df.mean()).fillna(df.mode().iloc[0])

#   col_1 col_2
#0    3.0     a
#1    2.0     a
#2    1.0     a
#3    2.0     d

If you have ties, the mode will be the one that is sorted first.


Solution 2:

What I will do

df.fillna(df.agg(['mean',lambda x : x.value_counts().index[0]]).ffill().iloc[-1,:])
   col_1 col_2
0    3.0     a
1    2.0     a
2    1.0     a
3    2.0     d

Post a Comment for "Replace Missing Values At Once In Both Categorical And Numerical Columns"