Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Merge and create a multi-index for duplicate columns

Tags:

python

pandas

I have two dataframes

sessions = DataFrame({"ID":[1,2,3,4,5],"2018-06-30":[23,34,45,67,75],"2018-07-31":[32,43,45,76,57]})
leads = DataFrame({"ID":[1,2,3,4,5],"2018-06-30":[7,10,28,15,30],"2018-07-31":[7,10,28,15,30]})

I wanna merge the two dataframes on ID and then create a multi-index to look like:

enter image description here

How can I do it?

a direct pandas.merge will create suffixes_x, _y which I do not want.

like image 828
Sharvari Gc Avatar asked Aug 29 '18 06:08

Sharvari Gc


1 Answers

Use concat with set_index by ID in both DataFrames and then swaplevel with sort_index for expected MultiIndex in columns:

df = (pd.concat([sessions.set_index('ID'), 
                leads.set_index('ID')], 
                axis=1, 
                keys=['sessions','leads'])
        .swaplevel(0,1,axis=1)
        .sort_index(axis=1, ascending=[True, False])
        )
print (df)
   2018-06-30       2018-07-31      
     sessions leads   sessions leads
ID                                  
1          23     7         32     7
2          34    10         43    10
3          45    28         45    28
4          67    15         76    15
5          75    30         57    30
like image 155
jezrael Avatar answered Oct 20 '22 00:10

jezrael