带有多个数据框的熊猫中的多索引

时间:2020-10-30 16:27:51

标签: python pandas dataframe multi-index

我试图用两个DataFrames在Pandas中创建一个多索引DataFrame。

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 

我试图制作一个新的DataFrame来对此进行管理

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

但这不起作用。有人知道吗?

1 个答案:

答案 0 :(得分:1)

您可以尝试使用concat,类似于other similar solution

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

A_expected的结果:

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