<分区>
这是我的数据框 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()
我没有得到正确的输出,任何帮助将不胜感激。谢谢
<分区>
这是我的数据框 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/