这是我的数据集,称它们为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
答案 0 :(得分:6)
在主题列上使用ne
和idxmax
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