So I have made a class that takes in a ticker symbol, and it returns a dataframe with all the price information for the dates specified. here is the code below:
import pandas as pd
import numpy as np
import pandas_datareader as pdr
# class to get stock price
class GetStockInfo():
'''
Class to retrieve stock info and returns it as a dataframe
'''
def __init__(self, ticker):
self.ticker = ticker.upper()
def build_df(self, start_date, end_date):
df = pd.DataFrame(pdr.DataReader(self.ticker, 'yahoo', start_date, end_date))
return df
now this works perfectly, but ideally id like to pass in a list of symbols and have it return a seperate df for each symbol. so for example,
symbols = ['aapl','googl','msft','tsla']
and id like it to return 4 seperate dfs, each named aapl_df, msft_df etc. is there any way to do this?
ive tried using a for loop like so
for i in symbols:
stock = GetStockInfo(i)
i_df = stock.build_df('2019-01-01', '2020-01-01')
but im not sure how to get it to return seperate dfs.
build_dffunction should be coded to take a list, and have some sort of for loop to iterate through the list and return a df for every item passed in?build_multiple_dfsand that basically can do theforloop andlist/dictlike shown in the answer. And then you just call this particular function!