Multiindex in Pandas with multiple Dataframes

Viewed 33

I tried to make a multiindex DataFrame in Pandas out of two DataFrames.

A1 =     
     col1  col2
0     1     3
1     2     4 

A2 =    
    col1  col2
0     5     6
1     7     8
A_expected=   
         tb1      tb2
     col1  col2 col1 col3
0     1     3    5    6
1     2     4    7    8 

I tried to make a new DataFrame to manage this

A_result=pd.DataFrame()
A_result['tb1']=A1
A_result['tb2']=A2

But this doesn't work. Has anyone an idea?

1 Answers

You can try with concat, something like in other similar solution:

A_expected = pd.concat([A1, A2], axis=1, keys=['tb1', 'tb2'])

Result of A_expected:

   tb1       tb2     
  col1 col2 col1 col2
0    1    3    5    6
1    2    4    7    8
Related