How to put the max of 3 separate columns in a new column in python/pandas

Question:

for example we have:

a b c
1 1 3
2 4 1
3 2 1

And now using python I’m trying to create this:

a b c max
1 1 3 3c
2 4 1 4b
3 2 1 3a
Asked By: van dam

||

Answers:

If need match first maximal column join max converted to strings with DataFrame.idxmax for columns names by max:

cols = ['a','b','c']
df['max'] = df[cols].max(axis=1).astype(str).str.cat(df[cols].idxmax(axis=1))
print (df)
   a  b  c max
0  1  1  3  3c
1  2  4  1  4b
2  3  2  1  3a

If possible multiple max values and need all matched columns use DataFrame.dot trick with separator ,:

print (df)
   a  b  c
0  3  1  3
1  2  4  1
2  3  2  1

cols = ['a','b','c']

max1 = df[cols].max(axis=1)
s = df[cols].eq(max1, axis=0).dot(pd.Index(cols) + ',').str[:-1]
df['max'] = max1.astype(str).str.cat(s)
    
print (df)
   a  b  c   max
0  3  1  3  3a,c
1  2  4  1    4b
2  3  2  1    3a
Answered By: jezrael

You can also use this:

cols = ['a', 'b', 'c']

df['max'] = df[cols].apply(lambda row: f"{row.max()}{row.idxmax()}", axis=1)
Answered By: Hamzah

idxmax(axis=1), method returns a Series with the index of the maximum value for each row.

assuming your dataframe is df.

df['max'] = df.idxmax(axis =1) + df.max(axis=1).map(str)
Answered By: Rupal Shah
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.