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.
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.
- Sets the date ranges:
start = datetime.datetime(2006,1,1)
end = datetime.datetime(2016,1,1)
- Specifies the symbols:
list_of_tickers = ['BAC','C','GS','JPM','MS','WFC']
- Iterates over each ticker, creates a single multilevel column dataframe:
p = data.DataReader(list_of_tickers, 'yahoo', start, end)
- OPTIONAL: Then pivot the 'symbols' column level and replaces the Date index so it can be used in analysis:
res = p.stack().reset_index()
- 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']