有人知道我可以用什么代码来代替下面的代码吗?我可以使用“startswith”而不是单独的where子句吗?我该怎么做
ds_4['New_Domain'] = np.where(ds_4['Domain'] == 'Alcohol,animals,children,crime', 'Alcohol', ds_4.Domain)
ds_4['New_Domain'] = np.where(ds_4['Domain'] == 'Alcohol,campaign-finance,ethics,public-service', 'Alcohol', ds_4.New_Domain)
ds_4['New_Domain'] = np.where(ds_4['Domain'] == 'Alcohol,candidates-biography', 'Alcohol', ds_4.New_Domain)
ds_4['New_Domain'] = np.where(ds_4['Domain'] == 'Alcohol,candidates-biography,crime', 'Alcohol', ds_4.New_Domain)
ds_4['New_Domain'] = np.where(ds_4['Domain'] == 'Alcohol,children', 'Alcohol', ds_4.New_Domain)
ds_4['New_Domain'] = np.where(ds_4['Domain'] == 'Alcohol,children,crime,public-health,public-safety', 'Alcohol', ds_4.New_Domain)
请尝试使用下面的
startswith
,因为startswith
允许将字符串或元组作为其第一个参数如果我正确理解了您试图实现的目标,您可以使用
apply
:这将得到一个新列
New_Domain
,如果Domain
中的相应值以“酒精”开头,则每行的值都将为“酒精”;否则,它将返回值Domain
相关问题 更多 >
编程相关推荐