首页 > 解决方案 > Python - 数据帧中第 2、第 3、第 n 大的 idxmax

问题描述

我正在使用以下命令来获取最大元素:

print("%.2f" % total_returns['return 5 days ago'].max(), total_returns['return 5 days ago'].idxmax(), "5 days max")

有没有办法让第二、第三等最大?

total_returns.head()
Out[15]: 
          first close last close  ... date 1250 days ago  date 1500 days ago
S&P 500       1469.25    2893.06  ...         2014-10-20          2013-10-22
Tesla           23.89     327.71  ...         2014-11-07          2013-11-11
Apple         3.67188     249.05  ...         2014-11-07          2013-11-11
Microsoft      58.375     144.19  ...         2014-11-07          2013-11-11
Amazon         76.125    1777.08  ...         2014-11-07          2013-11-11

[5 rows x 47 columns]

标签: pythondataframe

解决方案


我猜你正在使用pandas.DataFrame.

在这种情况下,您可以使用pandas.DataFrame.nlargest

n=3
total_returns.nlargest(n, 'return 5 days ago')

推荐阅读