从Python中的数据框的行中获取最大值 [英] Get max value from row of a dataframe in python

查看:784
本文介绍了从Python中的数据框的行中获取最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据框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 

这是我尝试过的

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

我没有得到正确的输出,任何帮助将不胜感激.谢谢

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

推荐答案

使用 max axis=1:

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

如果需要新列:

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

这篇关于从Python中的数据框的行中获取最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆