Search code examples
pythonpandasdataframepandas-datareader

Python - Concat List of Dataframes using DataReader


I'm trying to capture stock data using pandas_datareader. Given a handful of stock symbols, it's easy enough to just manually script out each DataReader call, then concat the result. However, if the length of the list grows to a much longer number, there has to be an easier way to iterate the process.

import pandas_datareader.data as web
import pandas as pd

symbols = ['AMZN','AAPL','MSFT','NFLX','GOOGL']
for i in symbols:
    print(i)

dfAMZN = web.DataReader('AMZN','yahoo')
dfAMZN['Symbol'] = 'AMZN'
dfAMZN['Date'] = dfAMZN.index

dfAAPL = web.DataReader('AAPL','yahoo')
dfAAPL['Symbol'] = 'AAPL'
dfAAPL['Date'] = dfAAPL.index

dfMSFT = web.DataReader('MSFT','yahoo')
dfMSFT['Symbol'] = 'MSFT'
dfMSFT['Date'] = dfMSFT.index

dfNFLX = web.DataReader('NFLX','yahoo')
dfNFLX['Symbol'] = 'NFLX'
dfNFLX['Date'] = dfNFLX.index

dfGOOGL = web.DataReader('GOOGL','yahoo')
dfGOOGL['Symbol'] = 'GOOGL'
dfGOOGL['Date'] = dfGOOGL.index

frames = [dfAMZN, dfAAPL, dfMSFT, dfNFLX, dfGOOGL]
dfStocks = pd.concat(frames)

Is there a way to iterate through the list of symbols and perform the steps below, instead of just printing i?


Solution

  • Use list comprehension with assign for new columns:

    symbols = ['AMZN','AAPL','MSFT','NFLX','GOOGL']
    frames = [web.DataReader(i,'yahoo').assign(Symbol = i, Date = lambda x: x.index) 
              for i in symbols]
    
    dfStocks = pd.concat(frames)
    

    Another alternative:

    symbols = ['AMZN','AAPL','MSFT','NFLX','GOOGL']
    
    frames = []
    for i in symbols:
        df = web.DataReader(i,'yahoo')
        df['Symbol'] = i
        df['Date'] = df.index
        frames.append(df)
    
    dfStocks = pd.concat(frames)