Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas fillna throws ValueError: fill value must be in categories

Tags:

Discription: both features are in categorical dtypes. and i used this code in a different kernal of same dateset was working fine, the only difference is the features are in flote64. later i have converted these feature dtypes into Categorical because all the features in the dataset represents categories.

Below is the code:

AM_train['product_category_2'].fillna('Unknown', inplace =True) AM_train['city_development_index'].fillna('Missing', inplace =True) 
like image 223
Ravi Varma Avatar asked Dec 07 '18 07:12

Ravi Varma


People also ask

What does Fillna () do in Python?

The fillna() method replaces the NULL values with a specified value. The fillna() method returns a new DataFrame object unless the inplace parameter is set to True , in that case the fillna() method does the replacing in the original DataFrame instead.

How do you use Panda Fillna?

To call the method, you simply type the name of your DataFrame, then a “.”, and then fillna() . Inside of the parenthesis, you can provide a value that will be used to fill in the missing values in the DataFrame.

What is Fillna Ffill?

Recap of Pandas' fillna method='ffill': Ffill or forward-fill propagates the last observed non-null value forward until another non-null value is encountered. method='bfill': Bfill or backward-fill propagates the first observed non-null value backward until another non-null value is met.

How do you fill a column in pandas?

Pandas has different methods like bfill, backfill or ffill which fills the place with value in the Forward index or Previous/Back respectively. axis: axis takes int or string value for rows/columns. Input can be 0 or 1 for Integer and ‘index’ or ‘columns’ for String.

What are the methods of pandas axis and bfill?

Pandas has different methods like bfill, backfill or ffill which fills the place with value in the Forward index or Previous/Back respectively. axis: axis takes int or string value for rows/columns. Input can be 0 or 1 for Integer and ‘index’ or ‘columns’ for String

What is the difference between pandas dropna () and fillna ()?

Just like pandas dropna () method manage and remove Null values from a data frame, fillna () manages and let the user replace NaN values with some value of their own. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

Why can't I run fillna on my Dataframe?

Load the original dataset without inplace=True, always before running the fillna secondtime. This problem arises because, you run the code twice, so fillna cannot be performed. Show activity on this post. In my case, I was using fillna on a dataframe with many features when I got that error.


2 Answers

Use Series.cat.add_categories for add categories first:

AM_train['product_category_2'] = AM_train['product_category_2'].cat.add_categories('Unknown') AM_train['product_category_2'].fillna('Unknown', inplace =True)   AM_train['city_development_index'] = AM_train['city_development_index'].cat.add_categories('Missing') AM_train['city_development_index'].fillna('Missing', inplace =True) 

Sample:

AM_train = pd.DataFrame({'product_category_2': pd.Categorical(['a','b',np.nan])}) AM_train['product_category_2'] = AM_train['product_category_2'].cat.add_categories('Unknown') AM_train['product_category_2'].fillna('Unknown', inplace =True)   print (AM_train)   product_category_2 0                  a 1                  b 2            Unknown 
like image 76
jezrael Avatar answered Oct 24 '22 02:10

jezrael


I was getting the same error in a data frame while trying to get rid of all the NaNs.
I did not look too much into it, but substituting .fillna() for .replace(np.nan, value) did the trick.
Use with caution, since I am not sure np.nan catches all the values that are interpreted as NaN

like image 39
lotrus28 Avatar answered Oct 24 '22 04:10

lotrus28