2016-12-30 63 views
2

我有一个框架的样子:如何合并两个数据框与MultiIndex?

  2015-12-30 2015-12-31 
300100 am 1    3 
     pm 3    2 
300200 am 5    1 
     pm 4    5 
300300 am 2    6 
     pm 3    7 

和其他框架的样子

  2016-1-1 2016-1-2 2016-1-3 2016-1-4 
300100 am 1   3   5   1 
     pm 3   2   4   5 
300200 am 2   5   2   6 
     pm 5   1   3   7 
300300 am 1   6   3   2 
     pm 3   7   2   3 
300400 am 3   1   1   3 
     pm 2   5   5   2 
300500 am 1   6   6   1 
     pm 5   7   7   5 

现在我想将两帧合并,并在合并后的框架中是这样的:

   2015-12-30 2015-12-31 2016-1-1 2016-1-2 2016-1-3 2016-1-4 
    300100 am 1   3   1   3   5   1 
      pm 3   2   3   2   4   5 
    300200 am 5   1   2   5   2   6 
      pm 4   5   5   1   3   7 
    300300 am 2   6   1   6   3   2 
      pm 3   7   3   7   2   3 
    300400 am       3   1   1   3 
      pm       2   5   5   2 
    300500 am       1   6   6   1 
      pm       5   7   7   5 

我试过pd.merge(frame1,frame2,right_index = True,left_index = True),但它返回的不是所需的格式。谁能帮忙?谢谢!

+0

什么是'frame1.index'和'frame2.index'?输出有什么问题? – jezrael

+0

frame1.index是MultiIndex(levels = [[300100.0,300200.0,300300.0],['am','pm']], labels = [[0,-1,1,-1,2,-1], [0,1,0,1,0,1]]) – Steven

+0

frame2.index是MultiIndex(levels = [[300100.0,300200.0,300300.0,300400.0,300500.0],['am','pm']], 标签= [[0,1,1,-1,2,-1,3,-1,4,-1],[0,1,0,1,0,1,0,1,0,1] ])。部分输出看起来像2016-02-01 00:00:00_x 2016-02-02 00:00:00_x \ 300100.0 am 1 3 300200.0 am 5 1 300300.0 am 2 6 – Steven

回答

1

您可以使用concat

print (pd.concat([frame1, frame2], axis=1)) 
      2015-12-30 2015-12-31 1.1.2016 2.1.2016 3.1.2016 4.1.2016 
300100 am   1.0   3.0   1   3   5   1 
     pm   3.0   2.0   3   2   4   5 
300200 am   5.0   1.0   2   5   2   6 
     pm   4.0   5.0   5   1   3   7 
300300 am   2.0   6.0   1   6   3   2 
     pm   3.0   7.0   3   7   2   3 
300400 am   NaN   NaN   3   1   1   3 
     pm   NaN   NaN   2   5   5   2 
300500 am   NaN   NaN   1   6   6   1 
     pm   NaN   NaN   5   7   7   5 

值在第一和第二列转换为float,因为NaN值转换intfloat - 看docs

一种可能的解决方案是通过一些int例如更换NaN0,然后转换为int

print (pd.concat([frame1, frame2], axis=1) 
     .fillna(0) 
     .astype(int)) 
      2015-12-30 2015-12-31 1.1.2016 2.1.2016 3.1.2016 4.1.2016 
300100 am   1   3   1   3   5   1 
     pm   3   2   3   2   4   5 
300200 am   5   1   2   5   2   6 
     pm   4   5   5   1   3   7 
300300 am   2   6   1   6   3   2 
     pm   3   7   3   7   2   3 
300400 am   0   0   3   1   1   3 
     pm   0   0   2   5   5   2 
300500 am   0   0   1   6   6   1 
     pm   0   0   5   7   7   5 
+0

非常感谢! – Steven

1

可以使用join

frame1.join(frame2, how='outer') 

enter image description here

+0

也谢谢! – Steven