gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:02:57 25 4
gpt4 key购买 nike

我也在进行简单的交易,需要一些帮助来将数据框连接在一起。直到现在,我的方法都行不通。

我的代码如下:

连接到 quantle API

 quandl.ApiConfig.api_key = 'xxxxxxxxxxxxxxx'

股票代码

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

用 panda 的引号创建一个面板对象 -> 创建一个 pandas DataFrame

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

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

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

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

 close = pd.concat(close1,close2)

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

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

最佳答案

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

应该这样做。

完整示例:

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)

输出:

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

关于python - 将两个 pandas 数据帧连接在一起(在 python 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47126285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com