python - 如何在python中更改数据框枢轴的布局
问题描述
我的数据框如下所示
Name | Date | Price | Disc%
A | 8/29 | 100.0 | 5
B | 8/29 | 88.80 | 6
A | 8/30 | 99.0 | 4
B | 8/30 | 85.0 | 3
如果我使用
pd.pivot_table(df, index='name',columns='Date',values=['price','disc']...),
我得到的顶部标题不同,首先显示价格的所有日期,然后显示折扣的所有日期。
Price || Disc%
----------------------------------
8/29 | 8/30 | 8/29 | 8/30
A 100 | 99.0 | 5 | 4
B 88.80| 85.0 | 6 | 3
我需要一个像下面这样的支点(列 - 顶部的日期)
8/29 || 8/30
----------------------------------
Price | Disc% | Price | Disc%
A 100 | 5 | 99.0 | 4
B 88.80| 6 | 85.0 | 3
Any suggestions appreciated.
请忽略上表中的任何格式错误或列标题
解决方案
pivoted.swaplevel(-1,-2, 1).sort_index(axis=1)
Date 8/29 8/30
Disc% Price Disc% Price
Name
A 5 100.0 4 99.0
B 6 88.8 3 85.0
推荐阅读
- ios - 为什么我在使用开发构建时调用 webView.load 时 Xcode Swift 应用程序崩溃?
- javascript - 在 Node.js 中让一个函数优先于另一个函数
- jquery - 通过ajax发送百里香列表
- javascript - React - 完成上传到 Firebase 后执行下一步
- php - PHP将数组数组合并为一个数组
- docker - 为什么主机的行为比 docker 容器更具确定性?
- ios - AVAudioEngine.inputNode.inputFormat(forBus: 0) changes after a couple of calls when AVPlayer is initialized
- python - UnboundLocalError:在 Python 中赋值之前引用的局部变量
- python - Python中的并行处理图像分析器功能
- flutter - 输入'未来
' 不是类型 'List 的子类型