我想根据以下条件创建一个带有数值的新列:
一种。 if gender is male & pet1=pet2, points = 5
if gender is male & pet1=pet2, points = 5
b。 if gender is female & (pet1 is 'cat' or pet1='dog'), points = 5
if gender is female & (pet1 is 'cat' or pet1='dog'), points = 5
C。所有其他组合,points = 0
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
我该怎么做?
为此,可以使用做np.where,条件使用位&和|用于and和or与周围的多个条件括号由于运算符优先级。因此,5返回条件为true的地方,0否则返回:
np.where
&
|
and
or
5
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