dee*_*esh 13 python max dataframe pandas
这是我的数据帧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
Run Code Online (Sandbox Code Playgroud)
我试图从数据帧的每一行获得最大值,我期待这样的输出
max_value
2
3.2
8.8
7.8
Run Code Online (Sandbox Code Playgroud)
这就是我尝试过的
df[len(df.columns)].argmax()
Run Code Online (Sandbox Code Playgroud)
我没有得到正确的输出,任何帮助将非常感激.谢谢
jez*_*ael 43
使用max有axis=1:
df = df.max(axis=1)
print (df)
0 2.0
1 3.2
2 8.8
3 7.8
dtype: float64
Run Code Online (Sandbox Code Playgroud)
如果需要新专栏:
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
Run Code Online (Sandbox Code Playgroud)
你可以用 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
44269 次 |
| 最近记录: |