How to iterate through pandas-datareader and create multiple dataframes for each stock ticker?

1.7k Views Asked by At

I want to iteratively create multiple dataframes from a list of ticker names.

Here is the stack overflow post i'm referencing:

Stack Overflow Post - Iteratively Create Multiple Dataframes

I'm having trouble understanding how to accomplish this, i feel like i'm missing or misunderstanding something here?

I wrote up the following list and dictionary

list_of_tickers = ['BAC','C','GS','JPM','MS','WFC']
dict_of_tickers = {name: pd.DataFrame() for name in list_of_tickers}

However when i run this portion of the code i get the following error:

for ticker, ticker_data in dict_of_tickers.items():
    ticker_data = data.DataReader(ticker,'yahoo',start,end)

This creates one single dataframe of all tickers but doesn't allow me to distinguish between them, i feel like i'm missing some key logic here.

2

There are 2 best solutions below

0
Lee On BEST ANSWER

I found out that the DataReader iterates over the list itself so the need to create a dictionary to iterate isn't necessary.

The following lines of code achieve what I was seeking which is an alternative to concatenating multiple dataframes from each stock ticker to avoid specifying DataReader for each symbol.

  1. Sets the date ranges:
start = datetime.datetime(2006,1,1)
end = datetime.datetime(2016,1,1)
  1. Specifies the symbols:
 list_of_tickers = ['BAC','C','GS','JPM','MS','WFC']
  1. Iterates over each ticker, creates a single multilevel column dataframe:
p = data.DataReader(list_of_tickers, 'yahoo', start, end)
  1. OPTIONAL: Then pivot the 'symbols' column level and replaces the Date index so it can be used in analysis:
res = p.stack().reset_index()
  1. OPTIONAL: This step isn't necessary and was purely for aesthetics to clean up the FrozenList and index names:
res.columns.names=[None]
res.index.names = ['ID']
3
jpp On

ticker_data is just a variable that's created and overwritten each iteration of your for loop. That doesn't help. To update your dictionary, assign a value to a key explicitly:

for ticker in dict_of_tickers:
    dict_of_tickers['ticker'] = data.DataReader(ticker, 'yahoo', start, end)

This assumes data.DataReader returns a dataframe. Notice we iterate keys, as the value (an empty dataframe) is not required for this assignment. In fact, you don't need to define a dictionary with empty dataframe values in the first place. Just use a single dictionary comprehension:

dict_of_tickers = {ticker: data.DataReader(ticker, 'yahoo', start, end) \
                   for ticker in list_of_tickers}