Reputation: 966
I am using a groupby with different aggregations per column like so
df = pd.DataFrame({'A': [1, 1, 2, 2],
'B': [1, 2, 3, 4],
'C': np.random.randn(4)})
df
A B C
0 1 1 0.362838
1 1 2 0.227877
2 2 3 1.267767
3 2 4 -0.562860
then
df.groupby('A').agg({'B': ['min', 'max'], 'C': 'sum'})
gives the output
B C
| min max | sum
A | |
1 | 1 2 | 0.590716
2 | 3 4 | 0.704907
I can now access then minimum of column B via
df['B']['min']
What I would like to do is simplify the headers to be
| B | C
1 | 1 | 0.590716
2 | 3 | 0.704907
where B is set to df['B']['min']
so I can call my columns again using df['B']
and df['C']
Upvotes: 1
Views: 122
Reputation: 448
Improved answer with help of DisneylandSC
Multiindex column can be first removed by using .drop()
and then .droplevel()
function.
df.drop(columns=['B','max'],level=1).droplevel(level=1,axis=1)
Old answer:
To remove multiindex column from your final dataset you can use .drop()
and specify level
.
df.drop(columns=['B','max'],level=1).T.droplevel(1).T
Upvotes: 1