Python DataFrames将多个数据帧放入单个数据框中

时间:2016-06-26 05:56:43

标签: python dataframe concat

导入所需的模块

import pandas.io.data as web
import datetime
import pandas as pd
import matplotlib.pyplot as plt
# Enable inline plotting
%matplotlib inline

设置股票的日期范围和股票代码

start = datetime.datetime(2016, 1, 1)
end = datetime.datetime.now()
Stock_List = ('BHP.Ax','AMP.AX','PRR.AX')
Stock_Code = "BHP.AX"
df_Stock_Code = pd.DataFrame()
Results = pd.DataFrame()

通过代码循环并获取数据

for Stock_Code in Stock_List:
     #Queries yahoo website for asx code froma start and end date
     f = web.DataReader(Stock_Code, 'yahoo', start, end)
     f['Stock_Code'] = Stock_Code 
     df_Stock_Code = f

将所有数据帧连接成on但由于某种原因失败

Results = [Results,df_Stock_Code] 

df_Results = pd.Concat(Results)

1 个答案:

答案 0 :(得分:0)

在原始代码中,df_Stock_Code被视为数据框列表。但是,在for loop中,正在为其分配当前数据帧,而不是将最新的数据帧附加到它。

结果是,在连接步骤中,df_Stock_Code只是一个引用Stock_List中最后一个库存的数据框。 Results也从未在循环中使用过,只是在开始时初始化时的空数据帧。

请改为尝试:

start = datetime.datetime(2016, 1, 1)
end = datetime.datetime.now()
Stock_List = ('BHP.Ax','AMP.AX','PRR.AX')
Stock_Code = "BHP.AX"
df_Stock_Code = pd.DataFrame()
Results = []

for Stock_Code in Stock_List:
    #Queries yahoo website for asx code froma start and end date
    f = web.DataReader(Stock_Code, 'yahoo', start, end)
    f['Stock_Code'] = Stock_Code 
    df_Stock_Code = Results.append(f)

df_Results = pd.concat(Results)
print df_Results