我的 df 在第一列有索引:
someD Amount Finals
9 14 8831 2726
10 09 9731 3728
11 22 5431 1724
.
.
.
我正在尝试返回一个元组,其中包含“Finals”列中的 max() 值和相应的索引。
到目前为止我有:
df.loc[df['Finals'].idxmax()]
返回整行和最大值(决赛)
someD 09
Amount 9731
Finals 3728
Name: 10, dtype: float64
如何返回形式为 (Index, Finals) 的元组,即:
mytuple = ('10', '3728')
最佳答案
使用Series.agg()方法:
In [49]: tuple(df['Finals'].agg(['idxmax','max']).values)
Out[49]: (10, 3728)
更新:使用 df.loc[]
:
In [74]: tuple(df.loc[[df['Finals'].idxmax()], 'Finals'].reset_index().values[0])
Out[74]: (10, 3728)
关于python - 列和索引的最大值作为元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48357259/