python - pandas data frame, max of columns while keeping index
问题描述
As output I want a dataframe with only 1 row (index should be the last date) and the maximum value by column.
A B C
2000-06-13 35.44110000 34.17990000 34.02230000
2000-06-14 92.11310000 91.05430000 90.95720000
2000-06-15 57.97080000 57.78140000 58.19820000
2000-06-16 34.17050000 92.45300000 58.51070000
I know I can use df.tails(n).max()
but that turns the entire thing into a Series which seems rather complicated to get back into a data frame.
Does anyone know something elegant or functional to accomplish it?
解决方案
Do you want idxmax
?
df.idxmax().to_frame().T
Output
A B C
0 2000-06-14 2000-06-16 2000-06-14
Or, per comments below.
df.max().to_frame().T
Output:
A B C
0 92.1131 92.453 90.9572
And,
df.max().to_frame().T.rename(index={0:df.idxmax().max()})
Output:
A B C
2000-06-16 92.1131 92.453 90.9572
推荐阅读
- spring - 选择实体的另一个属性的属性
- javascript - 如何单击一次并导航到请求页面?
- javascript - 在原生 JS 组件之间传递数据
- python - Pickle Error 13 Permission Denied 访问 os 制作的目录时
- r - 绘制 3D 椭圆抛物面的问题
- selenium - 打开新的浏览器选项卡
- apache-kafka - 如何在 Kafka 流中使用挂钟时间而不是事件时间来抑制窗口?
- c# - 如何使用来自另一个模型的交易值更新联系人的总值
- python - 将两个列表列表的整数相加
- swift - 在 Playgrounds 上使用 SpriteKit 声明背景后出现 Swift 错误