<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: codingandfunpython</title>
    <description>The latest articles on DEV Community by codingandfunpython (@codingandfun).</description>
    <link>https://dev.to/codingandfun</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F636453%2F2c5cac99-db0c-4978-98e9-d6cdbf9a7d73.png</url>
      <title>DEV Community: codingandfunpython</title>
      <link>https://dev.to/codingandfun</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codingandfun"/>
    <language>en</language>
    <item>
      <title>Profitability Margin Analysis with Python
</title>
      <dc:creator>codingandfunpython</dc:creator>
      <pubDate>Sun, 23 May 2021 20:11:15 +0000</pubDate>
      <link>https://dev.to/codingandfun/profitability-margin-analysis-with-python-20pd</link>
      <guid>https://dev.to/codingandfun/profitability-margin-analysis-with-python-20pd</guid>
      <description>&lt;p&gt;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&amp;nbsp;profitability margin analysis with Python&amp;nbsp;by comparing profitability ratios across peer companies. Below are the 5 different related profitability ratios that we will calculate and analyse:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Net Profit Margin&lt;/li&gt;
&lt;li&gt;  Gross Profit&lt;/li&gt;
&lt;li&gt;  Operating Profit&lt;/li&gt;
&lt;li&gt;  Return on Assets&lt;/li&gt;
&lt;li&gt;  Return on Equity&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi2.wp.com%2Fcodingandfun.com%2Fwp-content%2Fuploads%2F2021%2F05%2Fpexels-photo-6801874.jpeg%3Fresize%3D1806%252C1300%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi2.wp.com%2Fcodingandfun.com%2Fwp-content%2Fuploads%2F2021%2F05%2Fpexels-photo-6801874.jpeg%3Fresize%3D1806%252C1300%26ssl%3D1" alt="In this post, we use different profitability and margin ratios with Python in order to analyse profitability metrics across peer companies."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Photo by Anna Nekrashevich on&amp;nbsp;&lt;a href="https://www.pexels.com/photo/photo-of-person-holding-ceramic-mug-6801874/" rel="noopener noreferrer"&gt;Pexels.com&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Calculate Profitability Ratios
&lt;/h2&gt;

&lt;p&gt;Before starting with the Python code to identify peer companies and analyse profitability margins, we will introduce each of the ratios in this section.&amp;nbsp;&lt;/p&gt;

&lt;h3&gt;
  
  
  Profitability and Margin Ratios
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://corporatefinanceinstitute.com/resources/knowledge/finance/profitability-ratios/" rel="noopener noreferrer"&gt;Margin ratios&lt;/a&gt;&amp;nbsp;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:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Gross profit margin&amp;nbsp;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.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Gross Profit Margin = Gross Profit / Revenue&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://www.investopedia.com/terms/o/operatingmargin.asp" rel="noopener noreferrer"&gt;Operating profit margin&amp;nbsp;measures how good a company is able to generate profits&lt;/a&gt;&amp;nbsp;through its main operating activities:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Operating Profit Margin = Operating Profit / Revenue&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Net profit margin&amp;nbsp;indicates&amp;nbsp;&lt;a href="https://www.investopedia.com/ask/answers/021215/what-difference-between-gross-profit-margin-and-net-profit-margin.asp" rel="noopener noreferrer"&gt;how much a company makes in profits for each dollar of sales&lt;/a&gt;.&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Net Profit Margin = Net Income / Revenue&lt;/p&gt;

&lt;p&gt;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.&amp;nbsp;&lt;/p&gt;

&lt;p&gt;Once we find the best performers in terms of net margin within a group of peers, we could further&amp;nbsp;analyse gross profit margin&amp;nbsp;and operating margin to understand better why the top performer companies have an edge.&amp;nbsp;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&lt;a href="https://en.wikipedia.org/wiki/Cost_of_goods_sold" rel="noopener noreferrer"&gt;&amp;nbsp;Cost of Goods Sold&amp;nbsp;&lt;/a&gt;(higher gross profit margin) or lower Marketing &amp;amp; Sales, Research &amp;amp; Development and G&amp;amp;A (General and Administrative) expenses required to generate a dollar of sales.&lt;/p&gt;

&lt;p&gt;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&lt;a href="https://codingandfun.com/analysing-balance-sheet-financial-ratios-with-python/" rel="noopener noreferrer"&gt;analysing Balance Sheet Financial Ratios with Python&lt;/a&gt;.&amp;nbsp;&lt;/p&gt;

&lt;h2&gt;
  
  
  Return on Equity and Return on Assets
&lt;/h2&gt;

&lt;p&gt;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).&amp;nbsp;&lt;/p&gt;

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

&lt;p&gt;Similarly,&amp;nbsp;Return on Assets (ROA)&amp;nbsp;indicates how much assets a company employs to generate net income.&lt;/p&gt;

&lt;p&gt;ROE = Net Income / Average Total Equity&lt;/p&gt;

&lt;p&gt;ROA = Net Income / Average Total Assets&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Retrieving Profitability Ratios in Python
&lt;/h2&gt;

&lt;p&gt;This is my favourite part of the post. We will be using&amp;nbsp;&lt;a href="http://financialmodelingprep.com/" rel="noopener noreferrer"&gt;financialmodelingpre&lt;/a&gt;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:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; 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.&lt;/li&gt;
&lt;li&gt; Loop through each of the stocks to&amp;nbsp;retrieve profitability ratios&amp;nbsp;from the financial ratios API end point.&lt;/li&gt;
&lt;li&gt; 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.&lt;/li&gt;
&lt;li&gt; Convert the Python dictionary into a Pandas DataFrame&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;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.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;After running the code, below Pandas DataFrame will show the outcome of the&amp;nbsp;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi1.wp.com%2Fcodingandfun.com%2Fwp-content%2Fuploads%2F2021%2F05%2Fimage.png%3Fresize%3D1024%252C214%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi1.wp.com%2Fcodingandfun.com%2Fwp-content%2Fuploads%2F2021%2F05%2Fimage.png%3Fresize%3D1024%252C214%26ssl%3D1" alt="Profitability Ratios Python"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;Is it not great? Using less than 20 lines of Python code, we are able to compare profitability metrics across different peers companies.&amp;nbsp;&lt;/p&gt;

&lt;p&gt;We see that&amp;nbsp;Adobe&amp;nbsp;(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&amp;nbsp;&lt;a href="https://www.thebalancesmb.com/common-size-income-statement-analysis-an-example-393579" rel="noopener noreferrer"&gt;common size income statement&lt;/a&gt;&amp;nbsp;to look further into the operational expenses of the company.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi0.wp.com%2Fcodingandfun.com%2Fwp-content%2Fuploads%2F2021%2F05%2FProfitability-Ratios-by-Peer-2.png%3Fresize%3D1024%252C1024%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi0.wp.com%2Fcodingandfun.com%2Fwp-content%2Fuploads%2F2021%2F05%2FProfitability-Ratios-by-Peer-2.png%3Fresize%3D1024%252C1024%26ssl%3D1" alt="Profitability Analysis with Python"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope that you have find the post on how to perform a Profitability Analysis with Python useful.&amp;nbsp;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>datascience</category>
    </item>
  </channel>
</rss>
