1

My dataframe has mulitple columns. I find max value in each row. Now I want to know the which column is max in each row and I want to store it. Finally, I would like to know how many times this row was max (top), etc. My code:

df= 
    Aple  Bana  Oran
0    10   20   30
1    2    5    1  
2    8    9    4
3    45   21   18  
cols = df.columns.tolist
df['max_val'] = df[cols].max(axis=1)
df['max_col'] = df[df[cols]==df['max']] # store name of max value

Present output: Above df['max_val'] produced the desired output. The problem is with the df['max_col']. It failed to produced desired output

Expected output:

df= 
    Aple  Bana  Oran  max_val max_col
0    10   20   30     30      Oran
1    2    5    1      5       Bana 
2    8    9    4      9       Bana
3    45   21   18     45      Aple

print(df['max_col'].value_counts())
Bana 2
Oran 1
Aple 1
Mainland
  • 4,110
  • 3
  • 25
  • 56

1 Answers1

2

You can use idxmax:

df['max_val'], df['max_col'] = df.max(axis=1), df.idxmax(axis=1)

# also
df[cols].idxmax(axis=1)

Output:

   Aple  Bana  Oran  max_val max_col
0    10    20    30       30    Oran
1     2     5     1        5    Bana
2     8     9     4        9    Bana
3    45    21    18       45    Aple
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74