比方说,我有类别1到10,并且我想将red
分配到值3到5,到1,6和7,blue
到2,8,9和10。和10.熊猫:将多个类别合并为一个
我该怎么做?如果我尝试
df.cat.rename_categories(['red','green','blue'])
我得到一个错误:ValueError: new categories need to have the same number of items than the old categories!
但如果我把这个
df.cat.rename_categories(['green','blue','red', 'red', 'red'
'green', 'green', 'blue', 'blue' 'blue'])
我会得到一个错误,指出有重复的值。
我能想到的唯一的其他方法是编写一个for循环,它将通过值的字典并替换它们。有没有更优雅的解决这个问题?
您是否只有一个要更改的系列,还是必须在整个数据框中发生? (顺便说一句,'pd.cat'是'df.cat'的拼写错误吗?我们通常使用'pd'作为熊猫的缩写。) – DSM
抱歉,这是一种类型。它应该是一个dataFrame名称,我会解决它。但要回答你的问题,这只是一个系列/专栏。 –