Reputation: 143
df.fillna('unknown',inplace=True)
I have a dataframe df wherein some attributes are categorical and some are objects. Using the above expression I am trying to fill missing values in any of these columns with 'unknown'. But I get an erro while doing so saying 'unknown' is not present in categories of the categorical data columns.
How can I do it?
Upvotes: 1
Views: 260
Reputation: 18647
Use DataFrame.select_dtypes
and loop through each categorical column and add 'unknown' as a category using add_categories
method on the .cat
accessor.
for col in df.select_dtypes('category'):
df[col].cat.add_categories('unknown', inplace=True)
df.fillna('unknown',inplace=True)
Upvotes: 1