[英]How to change categorical variables in a for loop using a list with variable to change
I would like to simplify the number of categories for one variable.我想简化一个变量的类别数量。 The piece of code below is working:
下面的一段代码正在工作:
df.loc[(df['category'] == 'cat1')|(df['category'] == 'cat2')|(df['category'] == 'cat3')|...|(df['category'] == 'catn'),'category'] == 'other'
but I was wondering if I could do something like:但我想知道我是否可以做类似的事情:
category_to_change = ['cat1','cat2','cat3',...,'catn']
for name in category_to_change:
df.loc[(df['category'] == name),'category'] == 'other'
(this doesn't work) (这不起作用)
Any ideas how to do?任何想法如何做?
It is better if you provide extra code when asking a question, typically the code to create the dataframe, this helps to test suggestions.最好在提问时提供额外的代码,通常是创建 dataframe 的代码,这有助于测试建议。 This code should work:
此代码应该可以工作:
df = pd.DataFrame({'category': ['cat', 'dog', 'cat', 'rat']})
df['category'] = df['category'].replace(['cat', 'dog'], 'other')
All occurrences of cat
or dog
are replaced by other
.所有出现的
cat
或dog
都被other
替换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.