pandas ValueError:无法在具有新类别的Categorical上设置item,请先设置类别



现在,我正在通过将Yes替换为1和No替换为0来改变DataFrame内的信息。以前,我的代码工作得很好,现在我做了一些更改,由于内存问题。

<<p>之前代码/strong>得到下面提到的回溯错误
df.loc[df[df.decision == 'Yes'].index, 'decision'] = 1
df.loc[df[df.decision == 'No'].index, 'decision'] = 0

改变了

df.loc['Yes', "decision"] = 1
df.loc['No', "decision"] = 0

但是,问题还是一样的。

回溯

Traceback (most recent call last):
File "/snap/pycharm-community/226/plugins/python-ce/helpers/pydev/pydevd.py", line 1477, in _exec
pydev_imports.execfile(file, globals, locals)  # execute the script
File "/snap/pycharm-community/226/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"n", file, 'exec'), glob, loc)
File "/home/khawar/deepface/tests/Ensemble-Face-Recognition.py", line 148, in <module>
df.loc['Yes', "decision"] = 1
File "/home/khawar/.local/lib/python3.6/site-packages/pandas/core/indexing.py", line 670, in __setitem__
iloc._setitem_with_indexer(indexer, value)
File "/home/khawar/.local/lib/python3.6/site-packages/pandas/core/indexing.py", line 1763, in _setitem_with_indexer
isetter(loc, value)
File "/home/khawar/.local/lib/python3.6/site-packages/pandas/core/indexing.py", line 1689, in isetter
ser._mgr = ser._mgr.setitem(indexer=plane_indexer, value=v)
File "/home/khawar/.local/lib/python3.6/site-packages/pandas/core/internals/managers.py", line 543, in setitem
return self.apply("setitem", indexer=indexer, value=value)
File "/home/khawar/.local/lib/python3.6/site-packages/pandas/core/internals/managers.py", line 409, in apply
applied = getattr(b, f)(**kwargs)
File "/home/khawar/.local/lib/python3.6/site-packages/pandas/core/internals/blocks.py", line 1688, in setitem
self.values[indexer] = value
File "/home/khawar/.local/lib/python3.6/site-packages/pandas/core/arrays/categorical.py", line 2011, in __setitem__
"Cannot setitem on a Categorical with a new "
ValueError: Cannot setitem on a Categorical with a new category, set the categories first
python-BaseException

按照建议,我实现了新的代码

df['decision'] = (df['decision'] == 'Yes').astype(int)

回溯

Traceback (most recent call last):
File "/home/khawar/deepface/tests/Ensemble-Face-Recognition.py", line 174, in <module>
gbm = lgb.train(params, lgb_train, num_boost_round=1000, early_stopping_rounds=15, valid_sets=lgb_test)
File "/home/khawar/.local/lib/python3.6/site-packages/lightgbm/engine.py", line 231, in train
booster = Booster(params=params, train_set=train_set)
File "/home/khawar/.local/lib/python3.6/site-packages/lightgbm/basic.py", line 2053, in __init__
train_set.construct()
File "/home/khawar/.local/lib/python3.6/site-packages/lightgbm/basic.py", line 1325, in construct
categorical_feature=self.categorical_feature, params=self.params)
File "/home/khawar/.local/lib/python3.6/site-packages/lightgbm/basic.py", line 1123, in _lazy_init
self.__init_from_np2d(data, params_str, ref_dataset)
File "/home/khawar/.local/lib/python3.6/site-packages/lightgbm/basic.py", line 1162, in __init_from_np2d
data = np.array(mat.reshape(mat.size), dtype=np.float32)
ValueError: could not convert string to float: 'deepface/tests/dataset/029A33.JPG'

在你的解决方案中有categorical列,所以如果只替换一些行,熊猫想要输出列设置为分类,并且因为0,1不存在于分类中引发错误。

分类列的样本数据:

df = pd.DataFrame({'decision':['Yes','No']})
df['decision'] = pd.Categorical(df['decision'])

Series.mapcat.rename_categories的分类输出解:

df['decision1'] = df['decision'].map({'Yes':1, 'No':0})
df['decision2'] = df['decision'].cat.rename_categories({'Yes':1, 'No':0})

如果只有YesNo的值是可能的,通过Yes的比较重新创建所有的值,并将True, False1,0的映射转换为整数,如前面提到的@arhr,分类丢失:

df['decision3'] = (df['decision'] == 'Yes').astype(int)
print (df)
decision decision1  decision2 decision3
0      Yes         1          1         1
1       No         0          0         0
print (df.dtypes)
decision     category
decision1    category
decision2    category  
decision3       int32
dtype: object

当我试图运行包含不同数据类型的整个模型("category"one_answers"float64"。我解决了这个错误,只是替换了"category"列"字符串">

for col in df.select_dtypes(include=['category']).columns:
df[col] = df[col].astype('str')

最新更新