简体   繁体   中英

Appending results of for loop to pandas dataframe

I have tried a few different methods to store the results of the 'pandas-datareader' function returning a pandas dataframe object embedded in a for loop. The output continues overwriting despite use of the df.append method. Additionally, it would be beneficial if possible to suppress headers from subsequent calls to the DataReader function and only retain the numerical output.

for e in eqy[0:5]:

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

print prices

I found a potential solution that seems to work for my case but I am open to other answers suggesting quicker methods. I believe this current dataframe appending method is inefficient from a resource perspective.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM