pythonapijupyter-notebookyahoo-financepyalgotrade

Yahoo Finance to download fundamental data


Originally I was using IEX Cloud to download fundamental data:

api_url = f'https://sandbox.iexapis.com/stable/stock/{symbol}/quote?token={IEX_CLOUD_API_TOKEN}'
data = requests.get(api_url).json()
data

pe_ratio = data['peRatio']

However, I'm now using Yahoo Finance and I've successfully imported the library and data.

import yahoo_fin.stock_info as si
si.get_stats_valuation("msft")

How do I assign the PE, PS ratio, EV, etc. to a variable?


Solution

  • You may try below method:

    EV = si.get_stats_valuation("msft").iloc[1,1]
    
    Trailing_PE = si.get_stats_valuation("msft").iloc[2,1]
    
    Forward_PE = si.get_stats_valuation("msft").iloc[3,1]
    
    PS = si.get_stats_valuation("msft").iloc[5,1]
    

    I use iloc function to extract specific row or column data from dataframe.

    You may take a look on this "iloc" function from pandas library documentation.