我需要找到xrb
列中的最大值并打印此行(datetime
,xra
,最多(xrb
)。
以下是我的df
数据:
datetime xra xrb
1020 2016-02-21 17:00:00 1.000000e-09 2.380000e-07
1020 2016-02-21 17:00:01 1.000000e-09 2.380000e-07
1020 2016-02-21 17:00:02 1.000000e-09 2.380000e-07
1021 2016-02-21 17:01:00 1.080000e-09 2.370000e-07
1022 2016-02-21 17:02:00 1.000000e-09 2.350000e-07
1023 2016-02-21 17:03:00 1.000000e-09 2.340000e-07
1024 2016-02-21 17:04:00 1.000000e-09 2.330000e-07
1025 2016-02-21 17:05:00 1.000000e-09 2.330000e-07
我使用了这段代码:
print list(df.loc[df['xrb'].idxmax()])
但我只得到列表['datetime','xra','xrb']
有什么建议吗?
答案 0 :(得分:4)
您可以df
:
idxmax
倒置的[::-1]
print df
datetime xra xrb
1018 2016-02-21 17:00:00 1.000000e-09 2.380000e-07
1019 2016-02-21 17:00:01 1.000000e-09 2.380000e-07
1020 2016-02-21 17:00:02 1.000000e-09 2.380000e-07
1021 2016-02-21 17:01:00 1.080000e-09 2.370000e-07
1022 2016-02-21 17:02:00 1.000000e-09 2.350000e-07
1023 2016-02-21 17:03:00 1.000000e-09 2.340000e-07
1024 2016-02-21 17:04:00 1.000000e-09 2.330000e-07
print list(df.loc[df['xrb'][::-1].idxmax()])
[Timestamp('2016-02-21 17:00:02'), 1.0000000000000001e-09, 2.3800000000000001e-07]
编辑:
如果您有重复索引,请尝试查找所有行:
print df
datetime xra xrb
1020 2016-02-21 17:00:00 1.000000e-09 2.380000e-07
1020 2016-02-21 17:00:01 1.000000e-09 2.380000e-07
1020 2016-02-21 17:00:02 1.000000e-09 2.380000e-07
1021 2016-02-21 17:01:00 1.080000e-09 2.370000e-07
1022 2016-02-21 17:02:00 1.000000e-09 2.350000e-07
1023 2016-02-21 17:03:00 1.000000e-09 2.340000e-07
1024 2016-02-21 17:04:00 1.000000e-09 2.330000e-07
print df[df['xrb'] == df['xrb'].max()]
datetime xra xrb
1020 2016-02-21 17:00:00 1.000000e-09 2.380000e-07
1020 2016-02-21 17:00:01 1.000000e-09 2.380000e-07
1020 2016-02-21 17:00:02 1.000000e-09 2.380000e-07