这是我的数据集,称之为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
,第一个非期望值称为needNeed-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
最佳答案
在主题列上使用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
关于python - 如何在 Pandas 数据框上输出第一个非预期结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52201651/