将两个 Pandas 数据帧连接在一起(在 python 中)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47126285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:44:03  来源:igfitidea点击:

Concatenate two pandas data frames together (in python)

pythonpandasdataframeconcatenationstock

提问by Tom

I am working on a simple trading also and need some help with concatenating to data frames together. Until my now my approach does't work.

我也在做一个简单的交易,需要一些帮助来将数据帧连接在一起。直到我现在我的方法不起作用。

My code is the following:

我的代码如下:

Connect to quantle API

连接到 quantle API

 quandl.ApiConfig.api_key = 'xxxxxxxxxxxxxxx'

Ticker symbols

股票代码

ticker = ['FSE/ZO1_X',"FSE/WAC_X"]

Create a panel object with quotes from panda -> create a pandas DataFrame

用Pandas的引号创建一个面板对象 -> 创建一个Pandas数据帧

 df = quandl.get(ticker, start_date='2017-01-01', end_date='2017-11-03')

Slicing the close prices for each stock out of the panel data set

从面板数据集中切出每只股票的收盘价

close1 = df['FSE/ZO1_X - Close']
close2 = df['FSE/WAC_X - Close']

Concatenate the two data frames together - THIS STEP DOESN'T WORK

将两个数据帧连接在一起 - 这一步不起作用

 close = pd.concat(close1,close2)

The type of close1 and close 2 is pandas.core.series.Series.

close1 和 close 2 的类型是 pandas.core.series.Series。

How can I put close1 and close2 together, so that the index is the date and I have two additional columns with the close prices of stock 1 (close1) and stock 2 (close2) - similar like an ordinary excel sheet.

如何将 close1 和 close2 放在一起,以便索引是日期,并且我有两个附加列,其中包含股票 1 (close1) 和股票 2 (close2) 的收盘价 - 类似于普通的 Excel 表格。

回答by Omkar Neogi

close = pd.concat([close1, close2], axis=1)

should do it.

应该这样做。

Full example:

完整示例:

import pandas as pd 
import numpy as np

s = pd.Series([1,2,3,4,5])
t = pd.Series([11,12,13,14,15])
s = pd.concat([s,t], axis=1)
print(s)

Output:

输出:

   0   1
0  1  11
1  2  12
2  3  13
3  4  14
4  5  15