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)
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.
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.
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.
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.
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
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With