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
Results = [Results,df_Stock_Code]
df_Results = pd.Concat(Results)
答案 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