我有一个如下所示的数据帧
import pandas as pd
df1 = pd.DataFrame({'ethnicity': ['AMERICAN INDIAN/ALASKA NATIVE', 'WHITE - BRAZILIAN', 'WHITE-RUSSIAN','HISPANIC/LATINO - COLOMBIAN',
'HISPANIC/LATINO - MEXICAN','ASIAN','ASIAN - INDIAN','ASIAN - KOREAN','PORTUGUESE','MIDDLE-EASTERN','UNKNOWN',
'USER DECLINED','OTHERS']})
我想替换“种族”列的值。例如:如果值是ASIAN - INDIAN
,我只想将其替换为ASIAN
类似地,我想对包含AMERICAN
、WHITE
、HISPANIC
的字符串进行替换,其他字符串替换为others
。这就是我想要的
df1.loc[df.ethnicity.str.contains('WHITE'),'ethnicity'] = "WHITE"
df1.loc[df.ethnicity.str.contains('ASIAN'),'ethnicity'] = "ASIAN"
df1.loc[df.ethnicity.str.contains('HISPANIC'),'ethnicity'] = "HISPANIC"
df1.loc[df.ethnicity.str.contains('AMERICAN'),'ethnicity'] = "AMERICAN"
df1.loc[df.ethnicity.str.contains(other ethnicities),ethnicity] = "Others" # please note here I don't know how to replace all other ethnicities at once as others
我希望我的输出如下所示
按列表的值使用^{} ,for match返回} :
NaN
,因此添加^{或者你可以加入我们的行列:
相关问题 更多 >
编程相关推荐