参见英文答案 > Find the max of two or more columns with pandas 2个
data = {'name' : ['bill', 'joe', 'steve'],
'test1' : [85, 75, 85],
'test2' : [35, 45, 83],
'test3' : [51, 61, 45]}
frame = pd.DataFrame(data)
期望的输出:
name test1 test2 test3 Highscore
bill 75 75 85 85
joe 35 45 83 83
steve 51 61 45 61
有时
frame['Highscore'] = max(data['test1'], data['test2'], data['test3'])
有效,但大多数时候会出现此错误:
ValueError:具有多个元素的数组的真值是不明确的.使用a.any()或a.all()
为什么它有时只能起作用?还有另一种方法吗?
解决方法:
>>> frame['Highscore'] = frame[['test1','test2','test3']].max(axis=1)
>>> frame
name test1 test2 test3 Highscore
0 bill 85 35 51 85
1 joe 75 45 61 75
2 steve 85 83 45 85
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。