Profitability Margin Analysis with Python

Tags: python, programming, datascience
Published: 2021-05-23 20:11:15



Profitability ratios are financial metrics offering insights on how good a company is able to generate earnings from revenues, assets and equity. In this post, we will perform a profitability margin analysis with Python by comparing profitability ratios across peer companies. Below are the 5 different related profitability ratios that we will calculate and analyse:

-   Net Profit Margin
-   Gross Profit
-   Operating Profit
-   Return on Assets
-   Return on Equity

![In this post, we use different profitability and margin ratios with Python in order to analyse profitability metrics across peer companies.](https://i2.wp.com/codingandfun.com/wp-content/uploads/2021/05/pexels-photo-6801874.jpeg?resize=1806%2C1300&ssl=1)

Photo by Anna Nekrashevich on [Pexels.com](https://www.pexels.com/photo/photo-of-person-holding-ceramic-mug-6801874/)

How to Calculate Profitability Ratios
-------------------------------------

Before starting with the Python code to identify peer companies and analyse profitability margins, we will introduce each of the ratios in this section. 

### Profitability and Margin Ratios

[Margin ratios](https://corporatefinanceinstitute.com/resources/knowledge/finance/profitability-ratios/) are very useful to understand how efficient a company is able to transform revenues into operating and net income. We will cover three main metrics in this post, gross profit margin, operating margined net profit margin:

-   Gross profit margin is useful to know how much cost of goods sold are needed to generate one dollar of sales.The higher the gross profit margin, the more company flows to the operating income and net income from each dollar of revenue.

Gross Profit Margin = Gross Profit / Revenue

-   [Operating profit margin measures how good a company is able to generate profits](https://www.investopedia.com/terms/o/operatingmargin.asp) through its main operating activities:

Operating Profit Margin = Operating Profit / Revenue

-   Net profit margin indicates [how much a company makes in profits for each dollar of sales](https://www.investopedia.com/ask/answers/021215/what-difference-between-gross-profit-margin-and-net-profit-margin.asp). 

Net Profit Margin = Net Income / Revenue

When performing a financial analysis, we need to be aware that we can extract very insightful conclusions when comparing peers. For example, by comparing net profit margin across companies we can identify which companies are operating more efficiently in transforming sales into profits. 

Once we find the best performers in terms of net margin within a group of peers, we could further analyse gross profit margin and operating margin to understand better why the top performer companies have an edge. A higher operating margin indicates that a company has either very competitive gross profit margins or very attractive operating margins. I.e. able to generate a unit of sales spending less on[ Cost of Goods Sold ](https://en.wikipedia.org/wiki/Cost_of_goods_sold)(higher gross profit margin) or lower Marketing & Sales, Research & Development and G&A (General and Administrative) expenses required to generate a dollar of sales.

Alternatively to compare peer companies, we could perform a time series analysis instead in order to analyse a single company performance across time. This kind of ratio analysis is particularly useful to see company trends. I cover time series analysis of financial ratios in my previous post on[analysing Balance Sheet Financial Ratios with Python](https://codingandfun.com/analysing-balance-sheet-financial-ratios-with-python/). 

Return on Equity and Return on Assets
-------------------------------------

ROE or ROA are profitability ratios that measure how efficient a company is able to generate a profit based on the equity employed (ROE) or the amount of asset employed (ROA). 

We already saw in my previous post [how to analyse a company's return on equity with Python](https://codingandfun.com/analyze-a-firm-roe-with-python/). Therefore, we do not need to go into much details on how to interpret the Return on Equity (ROE) ratio. This financial ratio is simply letting us know how much net income a company generates based on the company shareholders equity. 

Similarly, Return on Assets (ROA) indicates how much assets a company employs to generate net income.

ROE = Net Income / Average Total Equity

ROA = Net Income / Average Total Assets

Great, we have covered enough theory. Lets see in the next section how can we first find peer companies and then retrieve profitability ratios using Python.

Retrieving Profitability Ratios in Python
-----------------------------------------

This is my favourite part of the post. We will be using [financialmodelingpre](http://financialmodelingprep.com/)p in order to retrieve and process the required financial data. The code presented in this post is very straightforward. Below are the high level steps that the script performs:

1.  Find all company peers. To do this step, we will rely on Financialmodelingprep. The API offers and end point to retrieve peers for a selected company. Therefore, we simply need to pass within the url the ticker of the company. Then, the API will return a Python list containing all peers based on the sector and market cap.
2.  Loop through each of the stocks to retrieve profitability ratios from the financial ratios API end point.
3.  Extract and add each of the financial ratios into a Python dictionary. This will be useful in order to be able to present the financial ratios by company.
4.  Convert the Python dictionary into a Pandas DataFrame

Note that we need an API key from financialmodelingprep in order for the code to work. You can get a free API key with 250 requests a day.

{% gist https://gist.github.com/codingandfunpython/70d5561d714674106116b1c19bc530f1 %}


After running the code, below Pandas DataFrame will show the outcome of the profitability analysis with Python. We get a financial table containing peers from Apple because that is the company that we have passed in our code. Feel free to change the ticker in order to perform a similar profitability analysis for other companies. We could also have appended Apple into the list in order to also have it included into the analysis.

![Profitability Ratios Python](https://i1.wp.com/codingandfun.com/wp-content/uploads/2021/05/image.png?resize=1024%2C214&ssl=1)


Wrapping Up
-----------

Is it not great? Using less than 20 lines of Python code, we are able to compare profitability metrics across different peers companies. 

We see that Adobe (ADBE) has a very nice net profit margin compared to other companies. We could observe that Adobe higher net profit margin is driven mainly by the high gross profit margin compared to the peers of around 87%. That is probably due to the fact that Adobe has digital products lacking physical substance and are relatively cheap to produce. However, the operating profit margin is in line with the rest of the peers. Probably due to the amount spend by Adobe in research and development of new products. We could build a [common size income statement](https://www.thebalancesmb.com/common-size-income-statement-analysis-an-example-393579) to look further into the operational expenses of the company.

As next steps, we could improve the Python script to add new functions. For example, we could compute the mean for each of the metrics in order to compare each of the companies to the average of the group.

![Profitability Analysis with Python](https://i0.wp.com/codingandfun.com/wp-content/uploads/2021/05/Profitability-Ratios-by-Peer-2.png?resize=1024%2C1024&ssl=1)

Hope that you have find the post on how to perform a Profitability Analysis with Python useful. 

Please note that any of the information included in this post is to be used as financial advise. The information presented here may be inaccurate and therefore should not be used to make investment decisions.