重命名Pandas中的分类

62lalag4  于 2022-12-21  发布在  其他
关注(0)|答案(1)|浏览(137)

我有以下类别:

Bathing 
Cooking 
Cleaning 
Cooking and Drinking 
Cooking, cleaning and drinking 
Domestic use

我想将它们重命名为大约3个类别,例如
x一个一个一个一个x一个一个二个x
所以我得到了这样的东西

df3.rename_categories({'cooking', 'drinking', 'other'})
pbpqsu0x

pbpqsu0x1#

我所理解和假设的是你想把任何相关的分类,

# Create a mapping of old category names to new category names
mapping = {
    'Bathing': 'Cleaning',
    'Cooking': 'Cooking',
    'Cleaning': 'Cleaning',
    'Cooking and Drinking': 'Other',
    'Cooking, cleaning and drinking': 'Other',
    'Domestic use': 'Other',
}

# Use the `rename` method to rename the categories
df3 = df3.rename(columns=mapping)

相关问题