I have a pandas DataFrame with 2 grouping columns and 3 numeric columns. I am grouping the data like this:
df = df.groupby(['date_week', 'uniqeid']).agg({
'completes':['sum', 'median', 'var', 'min', 'max']
,'dcount_visitors': ['sum', 'median', 'var', 'min', 'max']
,'dcount_visitor_groups': ['sum', 'median', 'var', 'min', 'max']
})
The result is the expected multi-level index:
MultiIndex(levels=[['completes', 'dcount_visitors', 'dcount_subscriptions', 'dcount_visitor_groups', 'date_week'], ['sum', 'median', 'var', 'min', 'max', '']],
labels=[[4, 3, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2], [5, 5, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4]])
Usually I flatten a multi-index like this:
df2 = df2.reset_index(drop=True)
However, when I inspect the columns I still get a multi-index.
I have tried including the as_index=False
in my groupby function but that doesn't work either.
Interestingly, this process works as expected if I only use 1 numeric column with one aggregation.
u = nunits.groupby(['account', 'week_date', 'accountid', 'full_account_name','SegmentName'], as_index=False).agg({'ConsumptionUnit': 'sum'})
Index(['account', 'week_date', 'accountid', 'full_account_name', 'SegmentName',
'ConsumptionUnit'],
dtype='object')
Any tips or recommendations would be appreciated.