比方說,我有類別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名稱,我會解決它。但要回答你的問題,這只是一個系列/專欄。 –