Pandas fillna 抛出 ValueError:填充值必须在类别中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53664948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas fillna throws ValueError: fill value must be in categories
提问by Ravi Varma
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.
描述:这两个特征都在分类 dtypes 中。我在相同日期集的不同内核中使用此代码工作正常,唯一的区别是功能在 flote64 中。后来我将这些特征 dtype 转换为 Categorical,因为数据集中的所有特征都代表类别。
Below is the code:
下面是代码:
AM_train['product_category_2'].fillna('Unknown', inplace =True)
AM_train['city_development_index'].fillna('Missing', inplace =True)
回答by jezrael
Use Series.cat.add_categories
for add categories first:
使用Series.cat.add_categories
了添加类别第一:
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