2016-11-20 141 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

相關問題