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:
How can I do it?
a direct pandas.merge will create suffixes_x, _y which I do not want.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With