gpt4 book ai didi

python - 尝试迭代并加入 Pandas DFs : AttributeError: 'Series' object has no attribute 'join'

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

我希望提取给定指数中约 200 种证券的历史数据。我从 csv 文件导入证券列表,然后遍历它们以从 quandl api 中提取它们各自的数据。每个证券的数据框有 12 列,因此我创建了一个新列,其中包含证券的名称和调整后的收盘价,以便稍后识别系列。

当我尝试将所有新列连接到一个空数据框中时收到错误消息。我收到一个属性错误:

'''
Print output data
'''
grab_constituent_data()
AttributeError: 'Series' object has no attribute 'join'

下面是我到目前为止用来到达这里的代码。

'''
Import the modules necessary for analysis
'''

import quandl
import pandas as pd
import numpy as np

'''
Set file pathes and API keys
'''

ticker_path = ''
auth_key = ''

'''
Pull a list of tickers in the IGM ETF
'''

def ticker_list():
df = pd.read_csv('{}IGM Tickers.csv'.format(ticker_path))
# print(df['Ticker'])
return df['Ticker']

'''
Pull the historical prices for the securities within Ticker List
'''

def grab_constituent_data():
tickers = ticker_list()
main_df = pd.DataFrame()

for abbv in tickers:
query = 'EOD/{}'.format(str(abbv))
df = quandl.get(query, authtoken=auth_key)
print('Competed the query for {}'.format(query))

df['{} Adj_Close'.format(str(abbv))] = df['Adj_Close'].copy()
df = df['{} Adj_Close'.format(str(abbv))]
print('Completed the column adjustment for {}'.format(str(abbv)))

if main_df.empty:
main_df = df
else:
main_df = main_df.join(df)

print(main_df.head())

最佳答案

看来在你这行

df = df['{} Adj_Close'.format(str(abbv))]

您得到的是 Serie 而不是 Dataframe。如果你想将你的系列转换为数据帧,你可以使用函数 to_frame() 像:

df = df['{} Adj_Close'.format(str(abbv))].to_frame()

我没有检查您的代码是否更简单,但这应该可以解决您的问题。

关于python - 尝试迭代并加入 Pandas DFs : AttributeError: 'Series' object has no attribute 'join' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40582073/

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