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

PythonPython 2.7PandasMax

Python Problem Overview


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

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

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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

Solution 3 - Python

if a max or min value between multiple columns in a df is to be determined then use:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2333196View Question on Stackoverflow
Solution 1 - PythonRoman PekarView Answer on Stackoverflow
Solution 2 - PythonalkoView Answer on Stackoverflow
Solution 3 - PythonVikas goelView Answer on Stackoverflow