小编典典

熊猫:如何基于多个条件为现有列分配值?

python

我想根据以下条件创建一个带有数值的新列:

一种。 if gender is male & pet1=pet2, points = 5

b。 if gender is female & (pet1 is 'cat' or pet1='dog'), points = 5

C。所有其他组合,points = 0

    gender    pet1      pet2
0   male      dog       dog
1   male      cat       cat
2   male      dog       cat
3   female    cat       squirrel
4   female    dog       dog
5   female    squirrel  cat
6   squirrel  dog       cat

我希望最终结果如下:

    gender    pet1      pet2      points
0   male      dog       dog       5
1   male      cat       cat       5
2   male      dog       cat       0
3   female    cat       squirrel  5
4   female    dog       dog       5
5   female    squirrel  cat       0
6   squirrel  dog       cat       0

我该怎么做?


阅读 190

收藏
2020-12-20

共1个答案

小编典典

为此,可以使用做np.where,条件使用位&|用于andor与周围的多个条件括号由于运算符优先级。因此,5返回条件为true的地方,0否则返回:

In [29]:
df['points'] = np.where( ( (df['gender'] == 'male') & (df['pet1'] == df['pet2'] ) ) | ( (df['gender'] == 'female') & (df['pet1'].isin(['cat','dog'] ) ) ), 5, 0)
df

Out[29]:
     gender      pet1      pet2  points
0      male       dog       dog       5
1      male       cat       cat       5
2      male       dog       cat       0
3    female       cat  squirrel       5
4    female       dog       dog       5
5    female  squirrel       cat       0
6  squirrel       dog       cat       0
2020-12-20