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:

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