python - 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]
解决方案
我猜你正在使用pandas.DataFrame
.
在这种情况下,您可以使用pandas.DataFrame.nlargest
n=3
total_returns.nlargest(n, 'return 5 days ago')
推荐阅读
- node.js - 云函数 - 使用循环保存数据
- performance - DAX 公式的 IF 表现
- javascript - 如何使用 Jest 测试忽略导入语句?
- vue.js - Vue Router Guard Method 仍然允许页面加载
- blockchain - 每个 Corda DLT 成员可以拥有自己的 CorDapp 来管理共享状态吗?
- c++ - 推导非类型模板参数的模板类型
- python - Pandas groupby:检查组内的差距
- c++ - 在编译时评估多态对象
- php - 添加到购物车按钮内的 Woocommerce 变化产品价格
- plot - 在 Grafana 上绘制不同时间范围的无时移值