I have two dataframes df1
and df2
:
In [56]: df1.head()
Out[56]:
col7 col8 col9
alpha0 D0 alpha0 D0 alpha0 D0
F35_HC_531d.dat 1.103999 1.103999 1.364399 1.358938 3.171808 1.946894
F35_HC_532d.dat 0.000000 0.000000 1.636934 1.635594 4.359431 2.362530
F35_HC_533d.dat 0.826599 0.826599 1.463956 1.390134 3.860629 2.199387
F35_HC_534d.dat 1.055350 1.020555 3.112200 2.498257 3.394307 2.090668
F52_HC_472d.dat 3.808008 2.912733 3.594062 2.336720 3.027449 2.216112
In [62]: df2.head()
Out[62]:
col7 col8 col9
alpha1 alpha2 alpha1 alpha2 alpha1 alpha2
filename
F35_HC_532d.dat 1.0850 2.413 0.7914 6.072000 0.8418 5.328
M48_HC_551d.dat 0.7029 4.713 0.7309 2.922000 0.7823 3.546
M24_HC_458d.dat 0.7207 5.850 0.6772 5.699000 0.7135 5.620
M48_HC_552d.dat 0.7179 4.783 0.6481 4.131999 0.7010 3.408
M40_HC_506d.dat 0.7602 2.912 0.8420 5.690000 0.8354 1.910
I want to concat these two dataframes. Notice that the outer column names are same for both so I only want to see 4 sub-columns in a new dataframe. I tried using concat as:
df = pd.concat([df1, df2], axis = 1, levels = 0)
But this produces a dataframe with columns named from col7
to col9
twice (so the dataframe has 6 outer columns). How can I put all the columns in level 1 under same outer column names?