Reputation: 4765
I have a MultiIndexed DataFrame:
import pandas as pd
import numpy as np
l0, l1 = ['A', 'B'],['a', 'b']
c0 = ['c1', 'c2', 'c3']
data = np.arange(12).reshape(4,3)
df = pd.DataFrame(data=data,
index=pd.MultiIndex.from_product([l0,l1]),
columns=c0)
>>>
c1 c2 c3
A a 0 1 2
b 3 4 5
B a 6 7 8
b 9 10 11
I want to transpose a level of the MultiIndex and of the columns so that I result in:
df2 = pd.DataFrame(index=pd.MultiIndex.from_product([l0, c0]),
columns=l1)
>>>
a b
A c1 NaN NaN
c2 NaN NaN
c3 NaN NaN
B c1 NaN NaN
c2 NaN NaN
c3 NaN NaN
And obviously I want to populate the right values. My solution is currently to use map with an iterator but it feels like Pandas would have some native way of doing this. Am I right, is there a better (faster) way?
from itertools import product
def f(df, df2, idx_1, col_0):
df2.loc[(slice(None), col_0), idx_1] = \
df.loc[(slice(None), idx_1), col_0].values
m = map(lambda k: f(df, df2, k[0], k[1]), product(l1, c0))
list(m) # <- to execute
>>> df2
>>>
a b
A c1 0 3
c2 1 4
c3 2 5
B c1 6 9
c2 7 10
c3 8 11
Upvotes: 17
Views: 10568
Reputation:
First stack the columns to bring c1, c2, and c3 to the index and then unstack the level that you want to become new columns (to bring a and b from the index to columns):
df.stack().unstack(level=1)
Out:
a b
A c1 0 3
c2 1 4
c3 2 5
B c1 6 9
c2 7 10
c3 8 11
Upvotes: 40