With a single-index data framework, columns are available in a group by object:
df1 = pd.DataFrame({'a':[2,2,4,4], 'b': [5,6,7,8]})
df1.groupby('a')['b'].sum() ->
a
2 11
4 15
But in the MultiIndex framework, when it is not grouped by level, the columns are no longer available in the group by object
df = pd.concat([df1, df1], keys=['c', 'd'], axis=1)
df ->
c d
a b a b
0 2 5 2 5
1 2 6 2 6
2 4 7 4 7
3 4 8 4 8
df.groupby([('c','a')])[('c','b')].sum() ->
KeyError: "Columns not found: 'b', 'c'"
This works as a workaround, but it is inefficient since it does not use the cpythonized aggregator, not to mention its uncomfortable look.
df.groupby([('c','a')]).apply(lambda df: df[('c', 'b')].sum())
Is there a way to access the MultiIndex column in the groupby object that I skipped?
source
share