尝试迭代并加入 Pandas DF:AttributeError: 'Series' 对象没有属性 'join'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40582073/
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
Trying to iterate and join Pandas DFs: AttributeError: 'Series' object has no attribute 'join'
提问by JMH
I'm looking to pull the historical data for ~200 securities in a given index. I import the list of securities from a csv file then iterate over them to pull their respective data from the quandl api. That dataframe for each security has 12 columns, so I create a new column with the name of the security and the Adjusted Close value, so I can later identify the series.
我希望提取给定指数中约 200 只证券的历史数据。我从 csv 文件中导入证券列表,然后遍历它们以从 quandl api 中提取它们各自的数据。每个证券的数据框有 12 列,因此我创建了一个包含证券名称和调整后收盘价的新列,以便稍后识别该系列。
I'm receiving an error when I try to join all the new columns into an empty dataframe. I receive an attribute error:
当我尝试将所有新列加入空数据帧时收到错误。我收到一个属性错误:
'''
Print output data
'''
grab_constituent_data()
AttributeError: 'Series' object has no attribute 'join'
Below is the code I have used to arrive here thus far.
下面是迄今为止我用来到达这里的代码。
'''
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())
采纳答案by hanego
It seems that in your line
看来,在你的行
df = df['{} Adj_Close'.format(str(abbv))]
you're getting a Serie and not a Dataframe. If you want to convert your serie to a dataframe, you can use the function to_frame() like:
你得到的是一个 Serie 而不是 Dataframe。如果要将系列转换为数据帧,可以使用 to_frame() 函数,例如:
df = df['{} Adj_Close'.format(str(abbv))].to_frame()
I didn't check if your code might be more simple, but this should fix your issue.
我没有检查您的代码是否更简单,但这应该可以解决您的问题。