2016-11-20 164 views
0

我尝试了几种不同的方法来存储返回嵌入在for循环中的熊猫数据框对象的'pandas-datareader'函数的结果。尽管使用了df.append方法,输出仍会继续覆盖。此外,如果可能的话,抑制随后调用DataReader函数的头并仅保留数字输出将是有益的。将for循环的结果附加到熊猫数据框中

for e in eqy[0:5]: 

    try: 
     prices = web.DataReader(e, 'google', start, end) 
     prices = pd.DataFrame.append(prices) 
    except: 
     pass 

print prices 

回答

0

我发现了一个潜在的解决方案,似乎适用于我的案件,但我打开其他答案,建议更快的方法。从资源的角度来看,我认为目前的数据框附加方法是低效的。

price_new = pd.DataFrame() 
for e in eqy[0:5]: 

    try: 
     prices = web.DataReader(e, 'google', start, end) 
     prices['Ticker'] = e 
     price_new = price_new.append(pd.DataFrame(prices)) 
    except: 
     pass 

print price_new 
+0

尝试追加数据框到Python列表。然后用'pd.concat()'将外部循环连接列表复制到最终的数据框中。 – Parfait