pythonpandasyahoo-financeyahooyahoo-api

get ESG scores from yesg


I am trying to get total esg scores from 2016-2021 for all companies in S&P500, but not every ticker has esg scores and some of them does not have data for 2016-2021.

My current code look like this:

import yesg
df = pd.DataFrame()
sp500 = pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')[0]
tickers = sp500['Symbol'].tolist()
for t in tickers:
  data=yesg.get_historic_esg(t)['Total-Score']
  df.append(data)

Please tell me how to get the data I need. Thanks in advance for your help.


Solution

  • You can try something like this :

    sp500 = pd.read_html("https://en.wikipedia.org/wiki/List_of_S%26P_500_companies")[0]
    tickers = sp500["Symbol"].tolist()
    
    scores_ok = []
    scores_ko = []
    for t in tickers:
        if (ser := yesg.get_historic_esg(t)) is not None:
            scores_ok.append(ser.loc[:, "Total-Score"].rename(t))
        else:
            scores_ko.append(t)
        
    df = pd.concat(scores_ok, axis=1)
    

    Output :

    #503 tickers | (448 OK, 55 KO)
    
    print(df)
    
                 MMM    AOS   ABT  ...   ZBH  ZION   ZTS
    Date                           ...                  
    2014-09-01  73.0    NaN  64.0  ...  61.0  47.0  47.0
    2014-10-01  72.0    NaN  64.0  ...  61.0  47.0  47.0
    2014-11-01  73.0    NaN  64.0  ...  61.0  47.0  47.0
    ...          ...    ...   ...  ...   ...   ...   ...
    2022-11-01   NaN    NaN   NaN  ...   NaN   NaN   NaN
    2022-12-01   NaN    NaN   NaN  ...   NaN   NaN   NaN
    2023-01-01   NaN  25.43   NaN  ...  27.3   NaN   NaN
    
    [101 rows x 448 columns]