简体   繁体   English

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

[英]How do I stack two DataFrames next to each other in Pandas?

I have two sets of stock data in DataFrames: 我在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

and I would like to stack them next two each other in a single DataFrame so I can access and compare columns (eg High) across stocks (GOOG vs. AAPL)? 我想在一个DataFrame中将它们相互堆叠在一起,这样我就可以访问和比较各股票(例如High)(GOOG与AAPL)? What is the best way to do this in Pandas and access the subsequent columns (eg GOOG's High column and AAPL's High column). 在Pandas中执行此操作的最佳方法是什么,并访问后续列(例如GOOG的High列和AAPL的High列)。 Thanks! 谢谢!

pd.concat is also an option 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

Have a look at the join method of dataframes, use the lsuffix and rsuffix attributes to create new names for the joined columns. 查看数据lsuffixjoin方法,使用lsuffixrsuffix属性为连接的列创建新名称。 It works like this: 它的工作原理如下:

>>> 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当索引是日期时间时,如何相互减去两个pandas日期时间系列DataFrame? - How do I subtract two pandas datetime series DataFrames from each other when the index is a datetime? Pandas 连接两个数据帧,两个数据帧中具有相同索引的列彼此相邻 - Pandas join two dataframes, columns with the same index from the two dataframes are next to each other 如何比较两个熊猫数据框并返回将它们相互映射的索引? - How can I compare two pandas dataframes and return an index mapping them to each other? 我如何在两个不同的熊猫数据框中相互比较值 - How can I compare values to each other in two different pandas dataframes 如何将两个Pandas Dataframe列彼此堆叠? - How do you stack two Pandas Dataframe columns on top of each other? 附加两个相邻的数据帧插入覆盖 - Append Two Dataframes next to each other insetad of overwriting 如何将两个 pandas 数据帧与它们在列中的值对齐? - How do I align two pandas dataframes on their values in a column? 如何使用 Pandas 中的子字符串匹配合并两个数据帧? - How do I merge two dataframes using a substring match in pandas? 如何合并两个 Pandas 数据帧并添加重叠列 - How do I merge two Pandas DataFrames and add the overlapping columns 如何在 Python 上匹配两列并合并熊猫数据框? - How do I match two columns and merge pandas dataframes on Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM