Published in

Analytics Vidhya

# How to calculate Price Book ratio with Python — Python for Finance

Price to Book ratio (PB) is a very useful metric to analyse the relative value of a company. PB helps to determine if a company price is currently undervalued or overvalued in comparison to similar firms. In this article, we are going to learn how to calculate Price Book value (PB) using Python.

# What is Price to Book Ratio?

Before moving to Python, it is always good to have a clear idea of what we want to do. In this case, we need to know what is the Price Book ratio and how it is calculated.

Price Book value is a financial ratio which can give to the investor a good indication of a firm price compare to other similar firms. It is calculated as a firm’s market capitalisation divided by the book value of equity.

An alternative method to calculate PB ratio is to divide the stock price by the book value of equity on a per share basis. We will use the first approach in our example.

There are only two elements required to calculate the Price Book Ratio:

• Company Market Capitalisation: It can be easily obtained from any exchange or financial website. It is the quote price per stock multiply by the number of shares outstanding.
• Book Value of Equity: It can be extracted from the financial reports published by public companies. It is found under the Equity section of the Balance Sheet under the label of total shareholders equity. The book value is simply the net assets of a company and it is a past looking measure. I.e. it takes into account historic transactions such as profit and losses from previous periods, dividends, etc.

# How to interpret Price Book Ratio?

Price Book ratio may be a good indicator to know if a firm is overvalued or undervalued compared to a peer company. A high Price Book ratio may indicate that the firm is expensive or maybe that the market is very optimistic about the firm future prospects. Growing firms tends to have a very high Price to Book ratio.

On the other hand, companies with low Price Book ratios are known as value stocks. It may indicate that a company is undervalued since the book value is higher than the market capitalisation. However, we need to be cautious with companies with low Price to Book ratios since it may also be a sign of financial distress or expected future earnings drop.

Firms in the technology industry are a clear example of growth companies with very high Price to Book ratio.

Let’s check the PB ratio with Python by calculating the ratio for companies in the technological sector.

# Calculating Price Book Ratio with Python

In order to get the required financial numbers for the calculation, I will use a free API containing stock and financial data. I recommend you to have a look at the documentation.

We will create a function, getpricetobook that will take as argument the ticker of the company for which we want to extract data. In our example, we will pass AAPL to get financials for Apple.

We make a request to the end point of our financial API to retrieve Balance Sheet statement data. Then, we store the response in a variable call BS.

`import requestsdef getpricetobook(stock):    BS = requests.get(f"https://financialmodelingprep.com/api/v3/financials/balance-sheet-statement/{stock}?period=quarter")    BS = BS.json()    print(BS)    getpricetobook('AAPL')`

If we print our BS variable, we will see that we have a json object that we need to parse. This is an easy task to do with Python. BS contains a dictionary with two keys. The second key, financials, contains a list of quarterly Balance Sheet statements:

First, we need to extract the Total shareholders equity value since that will represent ourBook value of equity (i.e. denominator of the Price Book ratio).

We can extract it by parsing BS and getting the total shareholder equity [‘Total shareholders equity’] included in the first element of the list [0] within the [‘financials’] key:

`book_value_equity_now = float(BS['financials'][0]['Total shareholders equity'])  print(book_value_equity_now)#returned number: 89531000000.0`

Note that we have included float in front of the API returned string in order to convert it to a float.

Next, we have to retrieve Apple’s market capitalisation. We can easily do that by making a request to the respective API end point:

`company_info = requests.get(f"https://financialmodelingprep.com/api/v3/company/profile/{stock}")company_info = company_info.json()market_cap =  float(company_info['profile']['mktCap'])print(market_cap)#market_cap value is 1423019080000.0`

As we did previously, we extract the mktCap value within the profile dictionary. Note that this value is based on the most current stock price while our equity book value is based on the latest available Balance Sheet Statement.

Finally, we can calculate our Price to Book ratio:

`price_to_book = market_cap/book_value_equity_nowreturn (price_to_book)`

# Putting the Python script together

Now, we have our Python script ready. We can pass any company ticker and Python will calculate the Price Book ratio for us. See below the complete code, and the calculation of the PB ratio for Apple, Microsoft, Ford and Tesla:

`import requests def getpricetobook(stock):    BS = requests.get(f"https://financialmodelingprep.com/api/v3/financials/balance-sheet-statement/{stock}?period=quarter")    BS = BS.json()    book_value_equity_now = float(BS['financials'][0]['Total shareholders equity'])          company_info = requests.get(f"https://financialmodelingprep.com/api/v3/company/profile/{stock}")    company_info = company_info.json()    market_cap =  float(company_info['profile']['mktCap'])    price_to_book = market_cap/book_value_equity_now    return (price_to_book)                                     print('AAPL:', getpricetobook('AAPL') )print('MSFT:', getpricetobook('MSFT') )print('Ford:', getpricetobook('F') )print('TSLA:', getpricetobook('TSLA') )#Return:AAPL: 15.89414928907306MSFT: 12.47125039733355Ford: 0.9288292681546861TSLA: 22.989353311258277`

# Wrapping up and Other Relative Valuation Measures

Great, we have now a script to calculate for us the Price to Book ratio. It was quite easy to build and may be useful to know if certain companies may be overvalued/ undervalued compared to a peer or industry group.

As next steps, I encourage you to calculate some other relative valuation measures such as Price Sales ratio and Price Earnings ratio by your own.

If you have liked the article, I recommend you to read my latest article on how to calculate Return on Equity (ROE) with Python since it can be used in combination with the Price Book value. High PB and low ROE may be a clear sign of overprice company. Low P/B and high ROE may be a good sign in terms of a cheap company with great prospects.

Originally published at https://codingandfun.com on February 13, 2020.

--

--

--

## More from Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

## Jose Manu (CodingFun)

Python for Finance. Learn step by step how to automate cool financial analysis tools.