31

This is my dataframe df

a     b     c
1.2   2    0.1
2.1   1.1  3.2
0.2   1.9  8.8
3.3   7.8  0.12

I'm trying to get max value from each row of a dataframe, I m expecting output like this

max_value
   2
  3.2
  8.8
  7.8 

This is what I have tried

df[len(df.columns)].argmax()

I'm not getting proper output, any help would be much appreciated. Thanks

Andre_k
  • 1,680
  • 3
  • 18
  • 41

2 Answers2

84

Use max with axis=1:

df = df.max(axis=1)
print (df)
0    2.0
1    3.2
2    8.8
3    7.8
dtype: float64

And if need new column:

df['max_value'] = df.max(axis=1)
print (df)
     a    b     c  max_value
0  1.2  2.0  0.10        2.0
1  2.1  1.1  3.20        3.2
2  0.2  1.9  8.80        8.8
3  3.3  7.8  0.12        7.8
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
6

You could use numpy

df.assign(max_value=df.values.max(1))

     a    b     c  max_value
0  1.2  2.0  0.10        2.0
1  2.1  1.1  3.20        3.2
2  0.2  1.9  8.80        8.8
3  3.3  7.8  0.12        7.8
piRSquared
  • 285,575
  • 57
  • 475
  • 624