Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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]

Solution 2:

You can create global or local variable like

globals()["TSLA"] = "some value"

print(TSLA)
locals()["TSLA"] = "some value"

print(TSLA)

but frankly it is waste of time. It is much more useful to keep it as dictionary.

With dictionary you can use for-loop to run some code on all dataframes.
You can also seletect dataframes by name. etc.

Examples:

df_max = {}

for name, df in df_.items():
    df_max[name] = df.max()
name = input("What to display: ")

df_[name].plot()

Post a Comment for "Iterrate And Save Each Stock Historical Data In Dataframe Without Downloading In CSV"