Reputation: 9762
Let's be given a table with a multi-level header:
cols = pd.MultiIndex.from_arrays([['a','a', 'b','b'], [0,1,0,1]],
names=('I','II'))
df = pd.DataFrame(np.random.rand(3,4),columns=cols)
df.index.name = 'idx'
I a b
II 0 1 0 1
idx
0 0.851031 0.294414 0.503343 0.081551
1 0.333798 0.965863 0.206981 0.898823
2 0.520647 0.868081 0.571291 0.275164
How can I "pivot" the first column index I
into the row index? To continue my example, I would like to achieve the following:
II 0 1
I idx
a 0 0.851031 0.294414
1 0.333798 0.965863
2 0.520647 0.868081
b 0 0.503343 0.081551
1 0.206981 0.898823
2 0.571291 0.275164
It is assured that the columns have the proper shape for such an operation.
Upvotes: 2
Views: 1551