Python Pandas 为所选列的行列最大值添加列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20033111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:18:55  来源:igfitidea点击:

Python Pandas add column for row-wise max value of selected columns

pythonpython-2.7pandasmax

提问by user2333196

data = {'name' : ['bill', 'joe', 'steve'],
    'test1' : [85, 75, 85],
    'test2' : [35, 45, 83],
     'test3' : [51, 61, 45]}
frame = pd.DataFrame(data)

I would like to add a new column that shows the max value for each row.

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

desired output:

所需的输出:

 name test1 test2 test3 HighScore
 bill  75    75    85    85
 joe   35    45    83    83 
 steve  51   61    45    61 

Sometimes

有时

frame['HighScore'] = max(data['test1'], data['test2'], data['test3'])

works but most of the time gives this error:

有效,但大多数情况下会出现此错误:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

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

Why does it only work sometimes? Is there another way of doing it?

为什么它只是有时有效?还有另一种方法吗?

采纳答案by Roman Pekar

>>> 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

回答by alko

>>> 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

回答by Vikas goel

if a maxor minvalue between multiple columns in a dfis to be determined then use:

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

df['Z']=df[['A','B','C']].apply(np.max,axis=1)