Hi,
If you are thinking of investing and also to create a perfect opportunity so that you can play with data in pandas, here is the use case I am working on.
Basically, from what I understood, if you want to value invest, there are two main parameters to take a look at before doing any other in depth research: P/B and P/E. Both of them show if the company has the potential to grow.
How can we retrieve these parameters using Python from Yahoo Finance for example … and the code that worked for me is as follows:
import yfinance as yf
import pandas as pd
payload=pd.read_html('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
first_table = payload[0]
second_table = payload[1]
df_sp = first_table
statscsv = open('stats.csv', 'a')
for value in df_sp['Symbol']:
stock = yf.Ticker(value)
if 'priceToBook' in stock.info:
statscsv.write(value+","+str(stock.info['priceToBook'])+","+str(stock.info['priceToSalesTrailing12Months'])+"\n")
statscsv.close()
I’ve been trying a lot to put the info directly in a pandas DataFrame and it did not work so for the purpose of querying the API only once, it makes a lot of sense to store it in an CSV file saved locally.
After it is saved locally you can manually load it to the DataFrame object by using (for my usage i manually added the column names into the file like Symbol,PB,PE at the beginning)
df_pb = pd.read_csv("stats.csv")
From what I saw, in some cases P/B data is not available in the output so the value ‘None’.
You can manually change that by replacing it with 0 and store it in a different DataFrame, like this
df_pb_clean = df_pb.replace({"None":"0"})
After you done this, you need to also convert the types of columns from object to float64 so that you can query specific values
df_pb_clean['PB'] = df_pb_clean['PB'].astype(float)
df_pb_clean['PE'] = df_pb_clean['PE'].astype(float)
After all of this is done, you can query it just as easy as
df_pb_green = df_pb_clean.query('0.0 < PB < 2.0')
And after that filter maybe also P/E for you use case.
The main goal is that we filter only the company with growth so that we can try to retrieve historical data and see main methods of analysis.
Cheers