Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge MultiIndex columns together into 1 level [duplicate]

Here's some data from another question:

date       type       value
1/1/2016   a          1
1/1/2016   b          2
1/1/2016   a          1
1/1/2016   b          4
1/2/2016   a          1
1/2/2016   b          1

Run this line of code:

x = df.groupby(['date', 'type']).value.agg(['sum', 'max']).unstack()

x should look like this:

         sum    max   
type       a  b   a  b
date                  
1/1/2016   2  6   1  4
1/2/2016   1  1   1  1

I want to combine the columns on the upper and lower level to get this:

           sum_a  sum_b   max_a  max_b
date                  
1/1/2016   2       6        1       4
1/2/2016   1       1        1       1

Is there an easy way to do this?

like image 824
cs95 Avatar asked Aug 25 '17 09:08

cs95


People also ask

Can you merge DataFrame on two columns?

How to join pandas dataframes on multiple columns? Note that, the list of columns passed must be present in both the dataframes. If the column names are different in the two dataframes, use the left_on and right_on parameters to pass your column lists to merge on.


2 Answers

There's discussion of this here:

Python Pandas - How to flatten a hierarchical index in columns

And the consensus seems to be:

x.columns = ['_'.join(col) for col in x.columns.values]
print(x)
          sum_a  sum_b  max_a  max_b
date                                
1/1/2016      2      6      1      4
1/2/2016      1      1      1      1

Would be nice if there was an inbuilt method for this, but there doesn't seem to be.

like image 107
greg_data Avatar answered Oct 15 '22 22:10

greg_data


Very similar solution to the above using zip:

x.columns = [x + '_' + i for x, i in zip(x.columns.get_level_values(0), x.columns.get_level_values(1))]
x
          sum_a  sum_b  max_a  max_b
date                                
1/1/2016      2      6      1      4
1/2/2016      1      1      1      1
like image 2
Andrew L Avatar answered Oct 16 '22 00:10

Andrew L