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_1col_2#03.0a#12.0a#21.0a#32.0d

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
03.0a12.0a21.0a32.0     d

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