2

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.

Lee
  • 95
  • 1
  • 13

2 Answers2

2

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']
Lee
  • 95
  • 1
  • 13
1

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}
jpp
  • 159,742
  • 34
  • 281
  • 339
  • Thank you i learned a great deal from your response, it appears however that for some reason DataReader returns as a dataframe when called by itself outside of the dictionary comprehension but when used as the value for the dictionary it returns a single column array value. Is there a way to extract the values as dataframes? – Lee Jan 20 '19 at 19:25
  • @Lee, No, I can't replicate. `data.DataReader` *can't* do "something different" within a dictionary comprehension versus outside. It's doing the same thing both times, but you should try and find out what (hint: `print(type(data.DataReader(...))` in your `for` loop). – jpp Jan 20 '19 at 21:46
  • 1
    Thank you, i appreciate your help! – Lee Jan 20 '19 at 23:01