Search code examples
pythonpandasyfinance

iterrate and save each stock historical data in dataframe without downloading in CSV


I would like to pull historical data from yfinance for a specific list of stocks. I want to store earch stock in a separate dataframes (each stock with its own df).

I can download it to multiple csv's through below code, but I couldn't find a way to store them in different dataframes (wihtout having to download them to csv)

import yfinance
    stocks = ['TSLA','MSFT','NIO','AAPL','AMD','ADBE','ALGN','AMZN','AMGN','AEP','ADI','ANSS','AMAT','ASML','TEAM','ADSK']

    for i in stocks:

        df = yfinance.download(i, start='2015-01-01', end='2021-09-12')
        df.to_csv( i + '.csv')

I want my end results to be a dataframe called "TSLA" for tsla historical data and another one called "MSFT" for msft data...and so on

I tried:

stock = ['TSLA','MSFT','NIO','AAPL','AMD']

df_ = {}     
for i in stock:
    df = yfinance.download(i, start='2015-01-01', end='2021-09-12')
    df_["{}".format(i)] = df 

And I have to call each dataframe by key to get it like df_["TSLA"] but this is not what I want. I need a datafram called only TSLA that have tsla data and so on. Is there a way to do it?


Solution

  • You don't need to download data multiple times. You just have to split whole data with groupby and create variables dynamically with locals():

    stocks = ['TSLA', 'MSFT', 'NIO', 'AAPL', 'AMD', 'ADBE', 'ALGN', 'AMZN',
              'AMGN', 'AEP', 'ADI', 'ANSS', 'AMAT', 'ASML', 'TEAM', 'ADSK']
    
    data = yfinance.download(stocks, start='2015-01-01', end='2021-09-12')
    
    for stock, df in data.groupby(level=1, axis=1):
        locals()[stock] = df.droplevel(level=1, axis=1)
        df.to_csv(f'{stock}.csv')
    

    Output:

    >>> TSLA
                 Adj Close       Close        High         Low        Open    Volume
    Date
    2014-12-31   44.481998   44.481998   45.136002   44.450001   44.618000  11487500
    2015-01-02   43.862000   43.862000   44.650002   42.652000   44.574001  23822000
    2015-01-05   42.018002   42.018002   43.299999   41.431999   42.910000  26842500
    2015-01-06   42.256001   42.256001   42.840000   40.841999   42.012001  31309500
    2015-01-07   42.189999   42.189999   42.956001   41.956001   42.669998  14842000
    ...                ...         ...         ...         ...         ...       ...
    2021-09-03  733.570007  733.570007  734.000000  724.200012  732.250000  15246100
    2021-09-07  752.919983  752.919983  760.200012  739.260010  740.000000  20039800
    2021-09-08  753.869995  753.869995  764.450012  740.770020  761.580017  18793000
    2021-09-09  754.859985  754.859985  762.099976  751.630005  753.409973  14077700
    2021-09-10  736.270020  736.270020  762.609985  734.520020  759.599976  15114300
    
    [1686 rows x 6 columns]
    
    >>> ANSS
                 Adj Close       Close        High         Low        Open  Volume
    Date
    2014-12-31   82.000000   82.000000   83.480003   81.910004   83.080002  304600
    2015-01-02   81.639999   81.639999   82.629997   81.019997   82.089996  282600
    2015-01-05   80.860001   80.860001   82.070000   80.779999   81.290001  321500
    2015-01-06   79.260002   79.260002   81.139999   78.760002   81.000000  344300
    2015-01-07   79.709999   79.709999   80.900002   78.959999   79.919998  233300
    ...                ...         ...         ...         ...         ...     ...
    2021-09-03  368.380005  368.380005  371.570007  366.079987  366.079987  293000
    2021-09-07  372.070007  372.070007  372.410004  364.950012  369.609985  249500
    2021-09-08  372.529999  372.529999  375.820007  369.880005  371.079987  325800
    2021-09-09  371.970001  371.970001  375.799988  371.320007  372.519989  194900
    2021-09-10  373.609985  373.609985  377.260010  372.470001  374.540009  278800
    
    [1686 rows x 6 columns]