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

标签 python python-3.x pandas

我希望提取给定指数中约 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/

相关文章:

Python正则表达式查找某个字符串后大括号内的所有内容

python-3.x - Python中二项分布的概率质量函数

python - 从前 2 行中选择特定单词,以特定单词、正则表达式开头

python - 如何找到重新排序的 numpy 数组的索引?

python - 我可以精确控制cythonize生成的.c文件的位置吗?

python - 使用 pandas 中的值重新采样 2d 坐标

python-3.x - 如何每 5 行后从列中选择值并将值赋给变量

python - 显示带有数据帧中的值的标签堆叠条形图

python - 不可排序的类型 : str() > float () in when filtering generator expression

python - 服务器端 COLLADA 转换器