Comparing Industry Profitability Ratios with Python

Profitability ratios are a good way to measure how effective companies are managing their operations. When looking into company profitability ratios such as gro

Comparing Industry Profitability Ratios with Python

Sep 11, 2023 5:20 PM - Rajnish Katharotiya

blog post cover photo

Image credit: Steven Van Elk

Profitability ratios are a good way to measure how effective companies are managing their operations. When looking into company profitability ratios such as gross profit margin, we need to compare them with industry data to evaluate the relative performance of the company.

In this post, we will calculate the median of the main profitability ratios for 8 different industries on companies operating in Nasdaq. We will then see how profitability ratios vary across industries.

Photo by studio sason from

What are Profitability Ratios?

With margin ratios we are able to measure how a company is able to generate income relative to sales. In general, all financial ratios facilitate the comparison across time periods and companies.

In this post, we will focus on below three profitability ratios which we can extract from the income statement.

Gross profit margin: This is the profit the company makes after deducting costs associated to make and sell products. Gross profit margin let us know how effective a company is to manufacture or purchase its products. Gross Profit Margin = Gross Profit / Sales

Operating profit margin. Once we get our profit margin, we can then move on and calculate the operating margin. That is the revenue from operations minus the operating costs that a company has to incur in order to be able to continue with its day to day operations. Operating profit margin let us know how effective the company is at operations. Operating Profit Margin = Operating Profit / Sales

Net income margin. It measure the net income produce by a company as a percentage of revenue. Net income margin is a good indicator of the overall effectiveness of the company. Net Income Margin = Net Income / Sales

Comparing Profitability Ratios Across Companies

These three ratios can be calculate for an individual company. But as with the rest of financial ratios, they need to be compared to other companies or across time in order to make sense from them.

That is, we need to compare the individual metrics of a company with an industry or comparable company to understand better how it is performing.

Therefore, what we will do in the next section using Python is the following:

  • Retrieve the income statement from 10 companies in 8 different sectors.

  • Calculate the gross profit, operating profit and net income margins ratios with Python for each individual company.

  • Compute the median gross profit margin, operating margin and net profit margin for each of the sectors

Profitability Ratios with Python

Lets move to the code. For our margin ratio calculations, we will use Company Financial Statements Endpoint.

First, we will need to define in a Python list, the list of sectors that we will be calculating the ratios for. Then, we also pass the number of companies that we want to include. To speed up the code, I will only include 10 companies per sector. Feel free to add many more to it.

import requests

import numpy as np

import pandas as pd

api_key = 'your api key'

sectors = ['Technology','Consumer Cyclical','Industrials','Basic Materials','Communication Services','Consumer Defensive','Healthcare','Real Estate','Utilities','Financial','Services']

exchange = 'NASDAQ'

marketcapmorethan = '1000000000'

number_of_companies = 10

symbols = []

overview_Nasdaq = {}

Once we have defined our code set up, we will need to make an API call to retrieve the 10 company tickers from the API for each of the industries. We will use a for loop to do so (see section 1 in the code) and append the extracted tickers to a symbol list.

Next, within the sector loop (section 2 in the code) we will loop through each of the tickers in order to retrieve the income statement from the last quarter. Remember, that our margin ratios are computed out of income statement items.

Then, in section 3 of the code below, we simply calculate the margin ratios and append the result to a list. Then, we convert the Python lists into Numpy arrays to calculate the median. That will give us the gross profit, operating profit and net margin medians for each of the industries.

Finally, in section 4, we add the median of each of the industry ratios into a Python dictionary.

Then, we convert it into a Pandas DataFrame in order to better visualise the data.

### 1 - request list of tickers for each industry

for sector in sectors:

symbols = []

gross_Margin = []

operating_Margin = []

net_Profit_Margin = []

overview_Nasdaq[sector] = {}

n = 0

screener = requests.get(f'{marketcapmorethan}=1000000000&volumeMoreThan=10000§or={sector}&exchange={exchange}&limit={number_of_companies}&apikey={api_key}').json()

for item in screener:


### 2 extract Income Statement for each of the companies

for company in symbols:


n = n + 1

IS = requests.get(f'{company}?period=quarter&apikey={api_key}').json()

### 3 compute margin ratios and append to list

gross_Margin.append(IS[0]['grossProfit'] / IS[0]['revenue'])

operating_Margin.append( (IS[0]['operatingIncome']) / IS[0]['revenue'])

net_Profit_Margin.append(IS[0]['netIncome'] / IS[0]['revenue'])

#NP to be able to calculate mean



gross_Margin = np.array(gross_Margin)

gross_Margin = np.median(gross_Margin)

operating_Margin = np.array(operating_Margin)

operating_Margin = np.median(operating_Margin)

net_Profit_Margin = np.array(net_Profit_Margin)

net_Profit_Margin = np.median(net_Profit_Margin)

### 4 - Add Ratios to a Dictionary and Convert to Pandas

overview_Nasdaq[sector]['Num. Companies'] = n

overview_Nasdaq[sector]['Gross Margin'] = gross_Margin * 100

overview_Nasdaq[sector]['Operating Margin'] = operating_Margin* 100

overview_Nasdaq[sector]['Net Profit Margin'] = net_Profit_Margin* 100

median_by_Industry = pd.DataFrame.from_dict(overview_Nasdaq,orient='columns')


The code above makes you able to compare different industries with each others and make

Other Blogs

Sep 11, 2023 - Rajnish Katharotiya

P/E Ratios Using Normalized Earnings

Price to Earnings is one of the key metrics use to value companies using multiples. The P/E ratio and other multiples are relative valuation metrics and they cannot be looked at in isolation. One of the problems with the P/E metric is the fact that if we are in the peak of a business cycle, earni...

blog post title

Sep 11, 2023 - Rajnish Katharotiya

What is Price To Earnings Ratio and How to Calculate it using Python

Price-to-Earnings ratio is a relative valuation tool. It is used by investors to find great companies at low prices. In this post, we will build a Python script to calculate Price Earnings Ratio for comparable companies. Photo by Skitterphoto on Pexels Price Earnings Ratio and Comparable Compa...

blog post title

Oct 17, 2023 - Davit Kirakosyan

VMware Stock Drops 12% as China May Hold Up the Broadcom Acquisition

Shares of VMware (NYSE:VMW) witnessed a sharp drop of 12% intra-day today due to rising concerns about China's review of the company's significant sale deal to Broadcom. Consequently, Broadcom's shares also saw a dip of around 4%. Even though there aren’t any apparent problems with the proposed solu...

blog post title


Financial Modeling Prep API provides real time stock price, company financial statements, major index prices, stock historical data, forex real time rate and cryptocurrencies. Financial Modeling Prep stock price API is in real time, the company reports can be found in quarter or annual format, and goes back 30 years in history.
2017-2023 © Financial Modeling Prep