我正在处理报告,需要创建数据透视表。
某些上下文:
因此,我的原始数据帧看起来像这样(一个小示例):
obs_date orig_date payments balance
2017-09-12 2019-09-12 200.00 1700.00
2019-09-19 2019-09-12 0.00 1750.00
2019-09-26 2019-09-12 100.00 1650.00
2019-09-19 2019-09-19 0.00 1235.00
2019-09-26 2019-09-19 300.00 950.00
2019-09-26 2019-09-26 50.00 3435.00
我尝试在数据帧上使用Pandas .pivot()
函数,但是得到的却是这样的:
payments balance
orig_date 2017-09-12 2017-09-19 2017-09-26 2017-09-12 2017-09-19 2017-09-26
obs_date
2017-09-12 200.00 0.00 100.00 1700.00 1750.00 1650.00
2019-09-19 0.00 300.00 1235.00 950.00
2019-09-26 50.00 3435.00
我想要得到的是这样的:
obs_date 2017-09-12 2017-09-19 2017-09-26
orig_date
2017-09-12 payments 200.00 0.00 100.00
balance 1700.00 1750.00 1650.00
2017-09-19 payments 0.00 300.00
balance 1235.00 950.00
2017-09-26 payments 50.00
balance 3435.00
那么,有没有办法做到这一点?
示例代码:
import pandas as pd
df = pd.DataFrame({
'obs_date':['2017-09-12', '2017-09-19', '2017-09-26', '2017-09-19', '2017-09-26', '2017-09-26'],
'orig_date': ['2017-09-12', '2017-09-12', '2017-09-12', '2017-09-19', '2017-09-19', '2017-09-26'],
'payments': [200.00, 0.00, 100.00, 0.00, 300.00, 50.00],
'balance': [1700.00,1750.00,1650.00,1235.00,950.00,3435.00]
})
# My unsuccesful attempt:
cols = [c for c in df.columns if c not in ['obs_date', 'orig_date']]
df_pivot = df.pivot(index='orig_date', columns='obs_date', values=cols)
答案 0 :(得分:3)
您非常接近通过stack
修复代码
df_pivot.stack(level=0)
Out[682]:
obs_date 2017-09-12 2019-09-19 2019-09-26
orig_date
2019-09-12 balance 1700.0 1750.0 1650.0
payments 200.0 0.0 100.0
2019-09-19 balance NaN 1235.0 950.0
payments NaN 0.0 300.0
2019-09-26 balance NaN NaN 3435.0
payments NaN NaN 50.0