Given a DataFrame (d
) with MultiIndex columns, I would like to set another DataFrame (d2
) as one of the 'multicolumns', such that the top level has some label, and the second level labels match those of the original:
nr.seed(0)
abc = ['a', 'b', 'c']
mi = pd.MultiIndex.from_product([['A'], abc])
d = DataFrame(np.random.randint(0, 10, (4, 3)), columns=mi)
d
A
a b c
0 5 0 3
1 3 7 9
2 3 5 2
3 4 7 6
d2 = DataFrame(np.random.randint(0, 10, (4, 3)), columns=abc)
d2
a b c
0 8 8 1
1 6 7 7
2 8 1 5
3 9 8 9
If possible, I would like to join them using a single builtin method that accomplishes the following forloop:
for c2 in d2:
d['B', c2] = d2[c2]
d
A B
a b c a b c
0 5 0 3 8 8 1
1 3 7 9 6 7 7
2 3 5 2 8 1 5
3 4 7 6 9 8 9
For a DataFrame with a single-level column:
d3 = d.copy()
d3.columns = d3.columns.droplevel(0)
d3 = d3.rename(columns=dict(zip('abc', 'def')))
d3
d e f
0 5 0 3
1 3 7 9
2 3 5 2
3 4 7 6
I can do the following:
d3[d2.columns] = d2
d3
d e f a b c
0 5 0 3 8 8 1
1 3 7 9 6 7 7
2 3 5 2 8 1 5
3 4 7 6 9 8 9
But when I try this with the MultiIndexed DataFrame, I get errors:
d['B', tuple(d2.columns)] = d2
=> ValueError: Wrong number of items passed 3, placement implies 1
d['B'][tuple(d2.columns)] = d2
=> KeyError: 'B'
Is there a builtin method to do this? (Basically do this for multiple columns at once).