从python中的数据帧的行中获取最大值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44300989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:53:34  来源:igfitidea点击:

Get max value from row of a dataframe in python

pythonpandasdataframemax

提问by deepesh

This is my dataframe df

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

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

回答by jezrael

Use maxwith axis=1:

使用maxaxis=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

回答by piRSquared

You could use numpy

你可以用 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