Imputation of missing values for categories in pandas

You can use df = df.fillna(df['Label'].value_counts().index[0]) to fill NaNs with the most frequent value from one column.

If you want to fill every column with its own most frequent value you can use

df = df.apply(lambda x:x.fillna(x.value_counts().index[0]))

UPDATE 2018-25-10

Starting from 0.13.1 pandas includes mode method for Series and Dataframes. You can use it to fill missing values for each column (using its own most frequent value) like this

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

Most of the time, you wouldn't want the same imputing strategy for all the columns. For example, you may want column mode for categorical variables and column mean or median for numeric columns.

For example:

df = pd.DataFrame({'num': [1.,2.,4.,np.nan],'cate1':['a','a','b',np.nan],'cate2':['a','b','b',np.nan]})

# numeric columns
>>> df.fillna(df.select_dtypes(include='number').mean().iloc[0], inplace=True)

# categorical columns
>>> df.fillna(df.select_dtypes(include='object').mode().iloc[0], inplace=True)

>>> print(df)

     num cate1 cate2
 0 1.000     a     a
 1 2.000     a     b
 2 4.000     b     b
 3 2.333     a     b

def fillna(col):
    col.fillna(col.value_counts().index[0], inplace=True)
    return col
df=df.apply(lambda col:fillna(col))

Tags:

Python

Pandas

R