【发布时间】:2015-05-18 11:32:44
【问题描述】:
如果我有一个数据框的多级列和多级索引
column_level1 a1 | a2
----+----|----+----
column_level2 b1 | b2 | b3 | b4
index1 | index2 | index3
-------+--------+--------+-----+----+----+-----
0 | c1 | d1 | 1 | 2 | 3 | 4 |
0 | c2 | d3 | 5 | 6 | 7 | 8 |
如何重塑我的数据框以将我的索引之一移动到 columns_level 之上? 假设我想要将当前的 index2 放在 column_level0 上。
我还需要一些有效的解决方案来解决这个问题。
我目前的解决方案是通过以下方式使用stack/unstack:
df.stack().stack().unstack(index2).unstack().unstack()
但是在巨大的数据帧上使用这种实现方式最终会消耗大量 RAM 并花费大量时间。
【问题讨论】: