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

标签 python pandas dataframe max

<分区>

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

我正在尝试从数据帧的每一行中获取最大值,我期待这样的输出

max_value
   2
  3.2
  8.8
  7.8 

这是我试过的

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

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

最佳答案

使用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 - 从python中的数据框行获取最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44300989/

相关文章:

python - 获取 Pandas 数据框中的节点祖先

apache-spark - Spark 数据帧 : Is it more efficient to filter during a join or after?

R 使用先前的迭代值对 FOR 循环进行矢量化

python - gnuplot multiplot 中的透明背景

python - 在 GAE 上启用带有 web2py 的设备

python - Bokeh DataTable - 在选择回调上返回行和列

python - Pandas 中 1 列中的 2 个数字相加和相减

python - 通过数组过滤DataFrame索引

python - 如何删除 pandas 中的 nan 值?

python - 使 OSX 中的 matplotlib 在虚拟环境中工作的最简单方法是什么?