Search code examples
pythonpython-3.xpandasdata-sciencepandas-datareader

Creating Multiple DataFrames with Pandas


Is there a way to use a 'for loop' or something similar to run through the code to create multiple DataFrames with Pandas that I can assign to separate variables, instead of hardcoding both DataFrames?

If I add new tickers it wouldn't be efficient to keep hardcoding them.

import pandas_datareader as pdr
from datetime import datetime

Equity_Tickers = ["FB", "MSFT"]

start = datetime(2018, 9, 15)
end = datetime.today().date()

# First DataFrame 
data = pdr.DataReader(Equity_Tickers[0], 'yahoo', start, end)
df = data[['Adj Close']]

# Second DataFrame
data = pdr.DataReader(Equity_Tickers[1], 'yahoo', start, end)
df1 = data[['Adj Close']]

Solution

  • You could certainly use a variable instead of hard-coding the ticker:

    ticker = Equity_Tickers[0]  # FB
    data = pdr.DataReader(ticker, 'yahoo', start, end)
    

    To create a loop, you could do something like:

    for ticker in Equity_Tickers:
        data = pdr.DataReader(ticker, 'yahoo', start, end)
        df = data[['Adj Close']]
    

    The tricky thing is storing the df variable in each iteration of the loop instead of overwriting it. One solution is to store each DataFrame in a list or dictionary.

    frames = []
    for ticker in Equity_Tickers:
        data = pdr.DataReader(ticker, 'yahoo', start, end)
        frames.append(data[['Adj Close']])
    
    # eg, use frames[0] to access first ticker's DataFrame
    

    OR

    frames = {}
    for ticker in Equity_Tickers:
        data = pdr.DataReader(ticker, 'yahoo', start, end)
        frames[ticker] = data[['Adj Close']]
    
    # eg, use frames['FB'] to access FB ticker's DataFrame
    

    If you're feeling really fancy you could also use a list comprehension / dictionary comprehension to accomplish this in one line:

    # list comprehension
    frames = [pdr.DataReader(t, 'yahoo', start, end)[['Adj Close']] for t in Equity_Tickers]
    
    # dict comprehension
    frames = {t: pdr.DataReader(t, 'yahoo', start, end)[['Adj Close']] for t in Equity_Tickers}
    

    Additional note: You might also consider combining the frames into one large DataFrame, perhaps a tidy-style DataFrame where "ticker" is a field for each row.