Python Pandas所选列的最大值

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 
 >>> frame['HighScore'] = frame[['test1','test2','test3']].apply(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