如何在熊猫数据框上输出第一个非预期结果

时间:2018-09-06 10:07:21

标签: python pandas dataframe

这是我的数据集,称它们为df

Id  Name   Math    Physics   Biology   Chemistry
1   Andy   A       B         A         B
2   Bert   B       C         D         A
3   Candy  A       A         A         B
4   Dony   A       A         C         B

期望值为A,第一个非期望值称为需求Need-improvement

Id  Name   Math    Physics   Biology   Chemistry  Need-Improvement
1   Andy   A       B         A         B          Physics       
2   Bert   B       C         D         A          Math 
3   Candy  A       A         A         B          Chemistry
4   Dony   A       A         C         B          Biology

1 个答案:

答案 0 :(得分:6)

在主题列上使用neidxmax

In [44]: subs = ['Math', 'Physics', 'Biology', 'Chemistry']

In [45]: df['Need-Improvement'] = df[subs].ne('A').idxmax(1)

In [46]: df
Out[46]:
   Id   Name Math Physics Biology Chemistry Need-Improvement
0   1   Andy    A       B       A         B          Physics
1   2   Bert    B       C       D         A             Math
2   3  Candy    A       A       A         B        Chemistry
3   4   Dony    A       A       C         B          Biology