gpt4 book ai didi

python - Pandas fillna 抛出 ValueError : fill value must be in categories

转载 作者:行者123 更新时间:2023-12-02 10:51:15 26 4
gpt4 key购买 nike

描述:这两个功能都是分类数据类型。我在相同的不同内核中使用了这段代码日期集工作正常,唯一的区别是功能位于 flote64 中。后来我将这些特征数据类型转换为分类因为数据集中的所有特征都代表类别。

下面是代码:

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

最佳答案

使用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)

示例:

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

关于python - Pandas fillna 抛出 ValueError : fill value must be in categories,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53664948/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com