pythonpandasdataframeyahoo-api

How do I generate a pandas dataframe consisting of the adjusted close prices of a few stocks from yahoo finance?


The df example end result I am looking for is the below, with the adjusted close prices as the data:

#               GE    JPM    MSFT     PG
#date                                   
#2015-01-02  25.06  62.49  46.760  90.44
#2015-01-05  24.60  60.55  46.325  90.01
#2015-01-06  24.07  58.98  45.650  89.60
#2015-01-07  24.08  59.07  46.230  90.07
#2015-01-08  24.37  60.39  47.590  91.10

import yfinance as yf
import datetime
import pandas as pd
import pandas_datareader as pdr

def get(tickers, startdate, enddate):
  def data(ticker):
    return (pdr.get_data_yahoo(ticker, start=startdate, end=enddate))
  datas = map (data, tickers)
  return(pd.concat(datas, keys=tickers, names=['Ticker', 'Date']))

ticker = ['GE' ,   'JPM' ,   'MSFT' ,    'PG'] 
all_data = get(ticker, datetime.datetime(2018, 10, 1), datetime.datetime(2020, 1, 1))

print (all_data)

                          High          Low  ...      Volume    Adj Close
Ticker Date                                  ...                         
GE     2018-10-01   229.419998   226.350006  ...  23600800.0   221.988266
       2018-10-02   230.000000   226.630005  ...  24788200.0   223.961426
       2018-10-03   233.470001   229.779999  ...  28654800.0   226.686707
       2018-10-04   232.350006   226.729996  ...  32042000.0   222.701340
       2018-10-05   228.410004   220.580002  ...  33580500.0   219.087158
...                        ...          ...  ...         ...          ...
JPM    2019-12-24  1350.260010  1342.780029  ...    347500.0  1343.560059
       2019-12-26  1361.327026  1344.469971  ...    667500.0  1360.400024
       2019-12-27  1364.530029  1349.310059  ...   1038400.0  1351.890015
       2019-12-30  1353.000000  1334.020020  ...   1050900.0  1336.140015
       2019-12-31  1338.000000  1329.084961  ...    961800.0  1337.020020

The above code stores the tickers in rows instead of columns.

I only need the adjusted close column for each ticker. I know how to extract the "adjusted close" column into another pandas series, but I get stuck on how to proceed further.


Solution

  • You can use pivot here:

    def get(tickers, startdate, enddate):
      def data(ticker):
        return (pdr.get_data_yahoo(ticker, start=startdate, end=enddate))
      datas = map (data, tickers)
      return(pd.concat(datas, keys=tickers, names=['Ticker', 'Date']))
    
    ticker = ['GE' ,   'JPM' ,   'MSFT' ,    'PG'] 
    all_data = get(ticker, datetime.datetime(2018, 10, 1), datetime.datetime(2020, 1, 1))
    
    df = all_data['Adj Close'].to_frame().reset_index()
    print(df.pivot(index='Date', columns='Ticker'))
    
    
                Adj Close
    Ticker             GE         JPM        MSFT          PG
    Date
    2018-10-01  11.550495  107.484100  112.812271   79.660583
    2018-10-02  11.770231  107.929184  112.363396   80.317520
    2018-10-03  11.923093  108.942482  112.382904   79.051262
    2018-10-04  12.095059  109.924706  110.060501   77.994453
    2018-10-05  12.591855  109.304848  109.416473   78.213432
    ...               ...         ...         ...         ...
    2019-12-24  11.178899  135.296478  156.515396  123.655876
    2019-12-26  11.218859  136.732239  157.798309  123.655876
    2019-12-27  11.168909  136.830582  158.086731  124.515007
    2019-12-30  11.069008  136.329041  156.724243  122.915245
    2019-12-31  11.148929  137.086258  156.833633  123.339874