I have a MultiIndex column Pandas DataFrame A:
foo
bar baz
s1_a 1 2
s1_b 3 4
s2_a 5 6
s2_b 7 8
I want to group the data on a key from another DataFrame B:
key
s1_a 1
s1_b 1
s2_a 2
s2_b 2
For a DataFrame without a MultiIndex, I would do:
pd.merge(A, B, left_index=True, right_index=True).groupby('key').sum()
but this does not work with the MultiIndex. The desired result is
foo
bar baz
1 4 6
2 12 14
How can I achieve this?