如何在Pandas中将两个DataFrame彼此相邻堆叠?

时间:2012-10-26 02:08:14

标签: python pandas

我在DataFrames中有两组库存数据:

> GOOG.head()
           Open   High    Low 
Date                                                                            
2011-01-03  21.01  21.05  20.78 
2011-01-04  21.12  21.20  21.05 
2011-01-05  21.19  21.21  20.90 
2011-01-06  20.67  20.82  20.55 
2011-01-07  20.71  20.77  20.27

AAPL.head()
              Open    High     Low
Date                              
2011-01-03  596.48  605.59  596.48
2011-01-04  605.62  606.18  600.12
2011-01-05  600.07  610.33  600.05
2011-01-06  610.68  618.43  610.05
2011-01-07  615.91  618.25  610.13

我希望在单个DataFrame中将它们相互堆叠在一起,这样我就可以访问和比较各个栏目(例如High)(GOOG与AAPL)?在Pandas中执行此操作的最佳方法是什么,并访问后续列(例如GOOG的High列和AAPL的High列)。谢谢!

2 个答案:

答案 0 :(得分:5)

pd.concat也是一个选项

In [17]: pd.concat([GOOG, AAPL], keys=['GOOG', 'AAPL'], axis=1)
Out[17]:
             GOOG                  AAPL
             Open   High    Low    Open    High     Low
Date
2011-01-03  21.01  21.05  20.78  596.48  605.59  596.48
2011-01-04  21.12  21.20  21.05  605.62  606.18  600.12
2011-01-05  21.19  21.21  20.90  600.07  610.33  600.05
2011-01-06  20.67  20.82  20.55  610.68  618.43  610.05
2011-01-07  20.71  20.77  20.27  615.91  618.25  610.13

答案 1 :(得分:3)

查看数据框的join方法,使用lsuffixrsuffix属性为已加入的列创建新名称。它的工作原理如下:

>>> x
          A         B         C
0  0.838119 -1.116730  0.167998
1 -1.143761  0.051970  0.216113
2 -0.614441  0.208978 -0.630988
3  0.114902 -0.248791 -0.503172
4  0.836523 -0.802074  1.478333
>>> y
          A         B         C
0 -0.455859 -0.488645 -1.618088
1 -2.295255  0.524681  1.021320
2 -0.484612  1.101463 -0.081476
3 -0.475076  0.915797 -0.998777
4 -0.847538  0.057044  1.053533
>>> x.join(y, lsuffix="_x", rsuffix="_y")
        A_x       B_x       C_x       A_y       B_y       C_y
0  0.838119 -1.116730  0.167998 -0.455859 -0.488645 -1.618088
1 -1.143761  0.051970  0.216113 -2.295255  0.524681  1.021320
2 -0.614441  0.208978 -0.630988 -0.484612  1.101463 -0.081476
3  0.114902 -0.248791 -0.503172 -0.475076  0.915797 -0.998777
4  0.836523 -0.802074  1.478333 -0.847538  0.057044  1.053533