我想基于以下条件创建一个具有数值的新列:
一个。如果color = blue& pet = dog,points = 10
湾如果color = blue& pet = cat,points = 8
℃。如果pet = snake&性别=女性,分数= 7
d。如果pet = mouse&性别=男性,分数= 6
所有行都必须满足其中一个条件。这些行永远不会满足2个条件。
color pet gender
0 blue dog male
1 blue cat male
2 orange snake female
3 green mouse male
我希望最终结果如下:
color pet gender points
0 blue dog male 10
1 blue cat male 8
2 orange snake female 7
3 green mouse male 6
我如何做到这一点?
答案 0 :(得分:3)
我认为使用loc
掩盖的4个独立作业可能更具可读性,而不是做一个很长的单行,
In [4]:
df.loc[(df['color']=='blue') & (df['pet']=='dog'), 'points'] = 10
df.loc[(df['color']=='blue') & (df['pet']=='cat'), 'points'] = 8
df.loc[(df['pet']=='snake') & (df['gender']=='female'), 'points'] = 7
df.loc[(df['pet']=='mouse') & (df['gender']=='male'), 'points'] = 6
df
Out[4]:
color pet gender points
0 blue dog male 10
1 blue cat male 8
2 orange snake female 7
3 green mouse male 6
将上述内容重写为多嵌套np.where
语句是可能的,但我发现一旦你达到3个以上的条件就会变得难以阅读,并且没有太多可以在单独的{{ {1}}陈述