由于绝望,我正在写这个问题。
我有以下df:
FORECAST_DATE BOOK_SEGMENT_ID PERIOD_END_P_n_L PERIOD_END_AQR_AVERAGE_BALANCE PERIOD_END_AQR_SPOT_BALANCE
0 Y-1 1 4563737.210000000000000000 33.567583016668937929 41.353567454639780837
1 Y-1 6 315406.100000000000000000 3.061750829952636692 2.522829568693654370
2 Y-1 4 15082237.880000000000000000 138.612207176403403592 181.732357082243759483
3 Y-1 3 43569174.010000000000000000 359.781133882318363070 566.852017680466803209
4 Y-1 2 494389977.810000000000000000 3706.315488833574075112 5882.954828234764105261
5 Y0 1 4626548.470000000000000000 33.463511078561497885 42.108630195163517588
6 Y0 4 57181865.440000000000000000 527.018846167262073635 720.788515365844542397
7 Y0 6 1580093.370000000000000000 14.088379421245762666 16.099449242348320563
8 Y0 3 179134588.050000000000000000 1456.303254133717454069 2216.535521013819977797
9 Y0 2 1845235987.940000000000000000 13967.821066500684233312 22474.822328214837177745
10 Y1 2 1845235987.940000000000000000 13967.821066500684233312 22474.822328214837177745
11 Y1 4 57181865.440000000000000000 527.018846167262073635 720.788515365844542397
12 Y1 3 179134588.050000000000000000 1456.303254133717454069 2216.535521013819977797
13 Y1 6 1580093.370000000000000000 14.088379421245762666 16.099449242348320563
14 Y1 1 4626548.470000000000000000 33.463511078561497885 42.108630195163517588
15 Y2 1 4626548.470000000000000000 33.463511078561497885 42.108630195163517588
16 Y2 2 1845235987.940000000000000000 13967.821066500684233312 22474.822328214837177745
17 Y2 4 57181865.440000000000000000 527.018846167262073635 720.788515365844542397
18 Y2 3 179134588.050000000000000000 1456.303254133717454069 2216.535521013819977797
19 Y2 6 1580093.370000000000000000 14.088379421245762666 16.099449242348320563
20 Y3 4 57181865.440000000000000000 527.018846167262073635 720.788515365844542397
21 Y3 1 4626548.470000000000000000 33.463511078561497885 42.108630195163517588
22 Y3 2 1845235987.940000000000000000 13967.821066500684233312 22474.822328214837177745
23 Y3 6 1580093.370000000000000000 14.088379421245762666 16.099449242348320563
24 Y3 3 179134588.050000000000000000 1456.303254133717454069 2216.535521013819977797
期望的结果是:
FORECAST_DATE
PERIOD_END_P_n_L Y-1 Y0 Y1 Y2 Y3
BOOK_SEGMENT_ID
1 4563737.210000000000000000 4626548.470000000000000000 4626548.470000000000000000 4626548.470000000000000000 4626548.470000000000000000
2 494389977.810000000000000000 1845235987.940000000000000000 1845235987.940000000000000000 1845235987.940000000000000000 1845235987.940000000000000000
3 43569174.010000000000000000 179134588.050000000000000000 179134588.050000000000000000 179134588.050000000000000000 179134588.050000000000000000
4 15082237.880000000000000000 57181865.440000000000000000 57181865.440000000000000000 57181865.440000000000000000 57181865.440000000000000000
6 315406.100000000000000000 1580093.370000000000000000 1580093.370000000000000000 1580093.370000000000000000 1580093.370000000000000000
PERIOD_END_AQR_AVERAGE_BALANCE
BOOK_SEGMENT_ID
1 4563737.210000000000000000 4626548.470000000000000000 4626548.470000000000000000 4626548.470000000000000000 4626548.470000000000000000
2 494389977.810000000000000000 1845235987.940000000000000000 1845235987.940000000000000000 1845235987.940000000000000000 1845235987.940000000000000000
3 43569174.010000000000000000 179134588.050000000000000000 179134588.050000000000000000 179134588.050000000000000000 179134588.050000000000000000
4 15082237.880000000000000000 57181865.440000000000000000 57181865.440000000000000000 57181865.440000000000000000 57181865.440000000000000000
6 315406.100000000000000000 1580093.370000000000000000 1580093.370000000000000000 1580093.370000000000000000 1580093.370000000000000000
PERIOD_END_AQR_SPOT_BALANCE
BOOK_SEGMENT_ID
1 4563737.210000000000000000 4626548.470000000000000000 4626548.470000000000000000 4626548.470000000000000000 4626548.470000000000000000
2 494389977.810000000000000000 1845235987.940000000000000000 1845235987.940000000000000000 1845235987.940000000000000000 1845235987.940000000000000000
3 43569174.010000000000000000 179134588.050000000000000000 179134588.050000000000000000 179134588.050000000000000000 179134588.050000000000000000
4 15082237.880000000000000000 57181865.440000000000000000 57181865.440000000000000000 57181865.440000000000000000 57181865.440000000000000000
6 315406.100000000000000000 1580093.370000000000000000 1580093.370000000000000000 1580093.370000000000000000 1580093.370000000000000000
请忽略这些值,它们应来自原始数据帧中的相应PERIOD_END_ {stat}。重要的是获取格式。
到目前为止,我已经尝试过:
forecast_summary.pivot(columns="FORECAST_DATE", index="BOOK_SEGMENT_ID")
RESULT:
PERIOD_END_P_n_L PERIOD_END_AQR_AVERAGE_BALANCE PERIOD_END_AQR_SPOT_BALANCE
FORECAST_DATE Y-1 Y0 Y1 Y2 Y3 Y-1 Y0 Y1 Y2 Y3 Y-1 Y0 Y1 Y2 Y3
BOOK_SEGMENT_ID
1 4563737.210000000000000000 4626548.470000000000000000 4626548.470000000000000000 4626548.470000000000000000 4626548.470000000000000000 33.567583016668937929 33.463511078561497885 33.463511078561497885 33.463511078561497885 33.463511078561497885 41.353567454639780837 42.108630195163517588 42.108630195163517588 42.108630195163517588 42.108630195163517588
2 494389977.810000000000000000 1845235987.940000000000000000 1845235987.940000000000000000 1845235987.940000000000000000 1845235987.940000000000000000 3706.315488833574075112 13967.821066500684233312 13967.821066500684233312 13967.821066500684233312 13967.821066500684233312 5882.954828234764105261 22474.822328214837177745 22474.822328214837177745 22474.822328214837177745 22474.822328214837177745
3 43569174.010000000000000000 179134588.050000000000000000 179134588.050000000000000000 179134588.050000000000000000 179134588.050000000000000000 359.781133882318363070 1456.303254133717454069 1456.303254133717454069 1456.303254133717454069 1456.303254133717454069 566.852017680466803209 2216.535521013819977797 2216.535521013819977797 2216.535521013819977797 2216.535521013819977797
4 15082237.880000000000000000 57181865.440000000000000000 57181865.440000000000000000 57181865.440000000000000000 57181865.440000000000000000 138.612207176403403592 527.018846167262073635 527.018846167262073635 527.018846167262073635 527.018846167262073635 181.732357082243759483 720.788515365844542397 720.788515365844542397 720.788515365844542397 720.788515365844542397
6 315406.100000000000000000 1580093.370000000000000000 1580093.370000000000000000 1580093.370000000000000000 1580093.370000000000000000 3.061750829952636692 14.088379421245762666 14.088379421245762666 14.088379421245762666 14.088379421245762666 2.522829568693654370 16.099449242348320563 16.099449242348320563 16.099449242348320563 16.099449242348320563
forecast_summary.set_index(["BOOK_SEGMENT_ID"], inplace=True).T
RESULT:
BOOK_SEGMENT_ID 1 6 4 3 2 1 4 6 3 2 2 4 3 6 1 1 2 4 3 6 4 1 2 6 3
FORECAST_DATE Y-1 Y-1 Y-1 Y-1 Y-1 Y0 Y0 Y0 Y0 Y0 Y1 Y1 Y1 Y1 Y1 Y2 Y2 Y2 Y2 Y2 Y3 Y3 Y3 Y3 Y3
PERIOD_END_P_n_L 4563737.210000000000000000 315406.100000000000000000 15082237.880000000000000000 43569174.010000000000000000 494389977.810000000000000000 4626548.470000000000000000 57181865.440000000000000000 1580093.370000000000000000 179134588.050000000000000000 1845235987.940000000000000000 1845235987.940000000000000000 57181865.440000000000000000 179134588.050000000000000000 1580093.370000000000000000 4626548.470000000000000000 4626548.470000000000000000 1845235987.940000000000000000 57181865.440000000000000000 179134588.050000000000000000 1580093.370000000000000000 57181865.440000000000000000 4626548.470000000000000000 1845235987.940000000000000000 1580093.370000000000000000 179134588.050000000000000000
PERIOD_END_AQR_AVERAGE_BALANCE 33.567583016668937929 3.061750829952636692 138.612207176403403592 359.781133882318363070 3706.315488833574075112 33.463511078561497885 527.018846167262073635 14.088379421245762666 1456.303254133717454069 13967.821066500684233312 13967.821066500684233312 527.018846167262073635 1456.303254133717454069 14.088379421245762666 33.463511078561497885 33.463511078561497885 13967.821066500684233312 527.018846167262073635 1456.303254133717454069 14.088379421245762666 527.018846167262073635 33.463511078561497885 13967.821066500684233312 14.088379421245762666 1456.303254133717454069
PERIOD_END_AQR_SPOT_BALANCE 41.353567454639780837 2.522829568693654370 181.732357082243759483 566.852017680466803209 5882.954828234764105261 42.108630195163517588 720.788515365844542397 16.099449242348320563 2216.535521013819977797 22474.822328214837177745 22474.822328214837177745 720.788515365844542397 2216.535521013819977797 16.099449242348320563 42.108630195163517588 42.108630195163517588 22474.822328214837177745 720.788515365844542397 2216.535521013819977797 16.099449242348320563 720.788515365844542397 42.108630195163517588 22474.822328214837177745 16.099449242348320563 2216.535521013819977797
我曾尝试使用.T .reset_index .set_index .pivot进行多种组合和变体,但未能获得正确的输出表。
如果您发布不同的组合,我可以尝试一下并说出是否有效并发布结果,如果您不愿意,则无需测试您的版本。
谢谢
更新:
我得到了这个接近的结果:
BOOK_SEGMENT_ID 1 2 3 4 6
FORECAST_DATE
PERIOD_END_P_n_L Y-1 4563737.210000000000000000 494389977.810000000000000000 43569174.010000000000000000 15082237.880000000000000000 315406.100000000000000000
Y0 4626548.470000000000000000 1845235987.940000000000000000 179134588.050000000000000000 57181865.440000000000000000 1580093.370000000000000000
Y1 4626548.470000000000000000 1845235987.940000000000000000 179134588.050000000000000000 57181865.440000000000000000 1580093.370000000000000000
Y2 4626548.470000000000000000 1845235987.940000000000000000 179134588.050000000000000000 57181865.440000000000000000 1580093.370000000000000000
Y3 4626548.470000000000000000 1845235987.940000000000000000 179134588.050000000000000000 57181865.440000000000000000 1580093.370000000000000000
PERIOD_END_AQR_AVERAGE_BALANCE Y-1 33.567583016668937929 3706.315488833574075112 359.781133882318363070 138.612207176403403592 3.061750829952636692
Y0 33.463511078561497885 13967.821066500684233312 1456.303254133717454069 527.018846167262073635 14.088379421245762666
Y1 33.463511078561497885 13967.821066500684233312 1456.303254133717454069 527.018846167262073635 14.088379421245762666
Y2 33.463511078561497885 13967.821066500684233312 1456.303254133717454069 527.018846167262073635 14.088379421245762666
Y3 33.463511078561497885 13967.821066500684233312 1456.303254133717454069 527.018846167262073635 14.088379421245762666
PERIOD_END_AQR_SPOT_BALANCE Y-1 41.353567454639780837 5882.954828234764105261 566.852017680466803209 181.732357082243759483 2.522829568693654370
Y0 42.108630195163517588 22474.822328214837177745 2216.535521013819977797 720.788515365844542397 16.099449242348320563
Y1 42.108630195163517588 22474.822328214837177745 2216.535521013819977797 720.788515365844542397 16.099449242348320563
Y2 42.108630195163517588 22474.822328214837177745 2216.535521013819977797 720.788515365844542397 16.099449242348320563
Y3 42.108630195163517588 22474.822328214837177745 2216.535521013819977797 720.788515365844542397 16.099449242348320563
通过以下方式获取它:
forecast_summary.pivot(columns="FORECAST_DATE", index="BOOK_SEGMENT_ID").T
现在将尝试立即交换FORECAST_DATE和BOOK_SEGMENT_ID。
UPDATE2:
在其他地方
forecast_summary.pivot(columns="FORECAST_DATE", index="BOOK_SEGMENT_ID").T.stack().reset_index(level=1).pivot(columns="FORECAST_DATE")
RESULT:
0
FORECAST_DATE Y-1 Y0 Y1 Y2 Y3
BOOK_SEGMENT_ID
PERIOD_END_AQR_AVERAGE_BALANCE 1 33.567583016668937929 33.463511078561497885 33.463511078561497885 33.463511078561497885 33.463511078561497885
2 3706.315488833574075112 13967.821066500684233312 13967.821066500684233312 13967.821066500684233312 13967.821066500684233312
3 359.781133882318363070 1456.303254133717454069 1456.303254133717454069 1456.303254133717454069 1456.303254133717454069
4 138.612207176403403592 527.018846167262073635 527.018846167262073635 527.018846167262073635 527.018846167262073635
6 3.061750829952636692 14.088379421245762666 14.088379421245762666 14.088379421245762666 14.088379421245762666
PERIOD_END_AQR_SPOT_BALANCE 1 41.353567454639780837 42.108630195163517588 42.108630195163517588 42.108630195163517588 42.108630195163517588
2 5882.954828234764105261 22474.822328214837177745 22474.822328214837177745 22474.822328214837177745 22474.822328214837177745
3 566.852017680466803209 2216.535521013819977797 2216.535521013819977797 2216.535521013819977797 2216.535521013819977797
4 181.732357082243759483 720.788515365844542397 720.788515365844542397 720.788515365844542397 720.788515365844542397
6 2.522829568693654370 16.099449242348320563 16.099449242348320563 16.099449242348320563 16.099449242348320563
PERIOD_END_P_n_L 1 4563737.210000000000000000 4626548.470000000000000000 4626548.470000000000000000 4626548.470000000000000000 4626548.470000000000000000
2 494389977.810000000000000000 1845235987.940000000000000000 1845235987.940000000000000000 1845235987.940000000000000000 1845235987.940000000000000000
3 43569174.010000000000000000 179134588.050000000000000000 179134588.050000000000000000 179134588.050000000000000000 179134588.050000000000000000
4 15082237.880000000000000000 57181865.440000000000000000 57181865.440000000000000000 57181865.440000000000000000 57181865.440000000000000000
6 315406.100000000000000000 1580093.370000000000000000 1580093.370000000000000000 1580093.370000000000000000 1580093.370000000000000000
可能很丑陋的代码行,但是现在需要弄清楚如何从列标题中删除0。
答案 0 :(得分:0)
过一会儿...
forecast_summary.set_index(["FORECAST_DATE", "BOOK_SEGMENT_ID"]).T.stack()
产生:
FORECAST_DATE Y-1 Y0 Y1 Y2 Y3
BOOK_SEGMENT_ID
PERIOD_END_P_n_L 1 4563737.210000000000000000 4626548.470000000000000000 4626548.470000000000000000 4626548.470000000000000000 4626548.470000000000000000
2 494389977.810000000000000000 1845235987.940000000000000000 1845235987.940000000000000000 1845235987.940000000000000000 1845235987.940000000000000000
3 43569174.010000000000000000 179134588.050000000000000000 179134588.050000000000000000 179134588.050000000000000000 179134588.050000000000000000
4 15082237.880000000000000000 57181865.440000000000000000 57181865.440000000000000000 57181865.440000000000000000 57181865.440000000000000000
6 315406.100000000000000000 1580093.370000000000000000 1580093.370000000000000000 1580093.370000000000000000 1580093.370000000000000000
PERIOD_END_AQR_AVERAGE_BALANCE 1 33.567583016668937929 33.463511078561497885 33.463511078561497885 33.463511078561497885 33.463511078561497885
2 3706.315488833574075112 13967.821066500684233312 13967.821066500684233312 13967.821066500684233312 13967.821066500684233312
3 359.781133882318363070 1456.303254133717454069 1456.303254133717454069 1456.303254133717454069 1456.303254133717454069
4 138.612207176403403592 527.018846167262073635 527.018846167262073635 527.018846167262073635 527.018846167262073635
6 3.061750829952636692 14.088379421245762666 14.088379421245762666 14.088379421245762666 14.088379421245762666
PERIOD_END_AQR_SPOT_BALANCE 1 41.353567454639780837 42.108630195163517588 42.108630195163517588 42.108630195163517588 42.108630195163517588
2 5882.954828234764105261 22474.822328214837177745 22474.822328214837177745 22474.822328214837177745 22474.822328214837177745
3 566.852017680466803209 2216.535521013819977797 2216.535521013819977797 2216.535521013819977797 2216.535521013819977797
4 181.732357082243759483 720.788515365844542397 720.788515365844542397 720.788515365844542397 720.788515365844542397
6 2.522829568693654370 16.099449242348320563 16.099449242348320563 16.099449242348320563 16.099449242348320563
首先,我设置两列的索引,然后转置将这两列作为列标题。然后,堆栈进入第二层并将其作为一列。