python - 列和索引的最大值作为元组

标签 python pandas

我的 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/

相关文章:

python - 在 python 新样式对象中使用 __setattr__ 时的最大递归深度?

python - 将代理图例句柄添加到现有图例

python-3.x - 从字符串中过滤用户名

sql - Pandas reset_index() 创建 level0 列

python - 在 groupby 和操作子数据帧之后保持其他列值不变

python - 使用 Python Tkinter : Always on top window isn't showing custom class tooltip text

python - 在 keras 层中包装 tensorflow 函数

python - 来自旧字典的新字典,将特定值与适当的键组合起来

pandas - Seaborn 和 Pandas : How to set yticks formatted as UK pounds

python - 交换数据框中列的内容