【问题标题】:Concatenate two pandas data frames together (in python)将两个熊猫数据框连接在一起(在python中)
【发布时间】:2017-11-05 20:26:59
【问题描述】:

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

我的代码如下:

连接到 quantle API

 quandl.ApiConfig.api_key = 'xxxxxxxxxxxxxxx'

股票代码

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

用 pandas 的引号创建一个面板对象 -> 创建一个 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]) 吗?
  • 解释.concat()的工作原理。

标签: python pandas dataframe concatenation stock


【解决方案1】:
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

【讨论】:

猜你喜欢
  • 2015-12-03
  • 2021-11-08
  • 2021-05-11
  • 1970-01-01
  • 2021-07-12
  • 2022-10-06
  • 2021-05-30
相关资源
最近更新 更多