Python将为选定列的行最大值添加列

Python将为选定列的行最大值添加列,python,python-2.7,pandas,max,Python,Python 2.7,Pandas,Max,我想添加一个新列,显示每行的最大值 期望输出: 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

我想添加一个新列,显示每行的最大值

期望输出:

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 
工作正常,但大多数情况下会出现以下错误:

ValueError:包含多个元素的数组的真值不明确。使用a.any()或a.all()


为什么它只在某些时候起作用?有其他方法吗?

如果要确定
df
中多列之间的
max
min
值,则使用:

frame['HighScore'] = max(data['test1'], data['test2'], data['test3'])
>>> 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

如果在按default计算max时忽略NA,则此方法效果更好。是否有方法获取列的名称。e、 g.HighScore=85在第一行中,该高分的列名为test1。我不知道(axis=1)的作用是什么?@RanjanR.Lamichhane简而言之,
max(axis=1)
获取行方向的最大值,而
max(axis=0)
获取按列显示的最大值。请看一下,如果我想在新列中写入最大列的名称,该怎么办?有关此特定操作的更快解决方案以及性能比较,请参阅。
>>> 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
df['Z']=df[['A','B','C']].apply(np.max,axis=1)