{"id":62657,"date":"2024-05-31T09:07:50","date_gmt":"2024-05-31T09:07:50","guid":{"rendered":"https:\/\/www.askpython.com\/?p=62657"},"modified":"2026-01-12T06:36:16","modified_gmt":"2026-01-12T06:36:16","slug":"financial-ratio-analysis-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/financial-ratio-analysis-python","title":{"rendered":"3\/5 &#8211; Financial Ratio Analysis Using Python"},"content":{"rendered":"\n<p>This is the third part of the series, where we compare companies&#8217; financials and evaluate their health. In this article, we will continue discussing how to utilize the Python programming language and its libraries to study different companies&#8217; profit and loss statements. We will also discuss different ratios used for a company&#8217;s financial analysis and understand what these ratios tell us about the particular company.<\/p>\n\n\n\n<div class=\"wp-block-group has-ast-global-color-5-color has-ast-global-color-8-background-color has-text-color has-background has-link-color wp-elements-2f4963bc878772eb276cc402ca858e04 is-layout-constrained wp-container-core-group-is-layout-b61869db wp-block-group-is-layout-constrained\" style=\"border-width:1px;border-radius:10px;margin-top:var(--wp--preset--spacing--20);margin-bottom:var(--wp--preset--spacing--20);padding-top:var(--wp--preset--spacing--50);padding-right:var(--wp--preset--spacing--50);padding-bottom:var(--wp--preset--spacing--50);padding-left:var(--wp--preset--spacing--50)\">\n<h2 class=\"wp-block-heading has-ast-global-color-5-color has-text-color has-link-color wp-elements-cfe684ec3f2d44b3c9251d3534b74c88\">Python Financial Analysis Series<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.askpython.com\/python\/fundamental-financial-analysis-python\" data-type=\"post\" data-id=\"62628\">Fundamental Analysis: Using Python for Stock Evaluation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.askpython.com\/python\/examples\/python-pl-statement-analysis\" data-type=\"post\" data-id=\"62635\">Comparing Financial Performance of Companies with Python<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.askpython.com\/python\/examples\/financial-ratio-analysis-python\" data-type=\"post\" data-id=\"62657\">Financial Ratio Analysis Using Python<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.askpython.com\/python\/examples\/python-balance-sheet-analysis\" data-type=\"post\" data-id=\"62636\">Analyzing a Balance Sheet with Python<\/a><\/li>\n<\/ol>\n<\/div>\n\n\n\n<p><strong><em>Recommended: <a href=\"https:\/\/www.askpython.com\/python\/examples\/relative-strength-index-rsi\">(3\/5) Relative Strength Index (RSI): A Powerful Trading Indicator Implemented in Python<\/a><\/em><\/strong><\/p>\n\n\n\n<p><strong><em>Recommended: <a href=\"https:\/\/www.askpython.com\/python\/examples\/macd-indicator-python\">(4\/5) MACD Indicator: Python Implementation and Technical Analysis<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Analyzing Key Financial Ratios<\/h2>\n\n\n\n<p>In this article, we will discuss the ratios related to the Profit and loss statement, which can tell a lot about the company&#8217;s financial health. While many fintech experts, such as the team at\u00a0<a href=\"https:\/\/dashdevs.com\/\" target=\"_blank\" rel=\"noopener\">DashDevs<\/a>, emphasize the importance of automated data processing for real-time insights, we can achieve a similar foundational understanding by calculating these ratios ourselves. The ratios we will examine are Gross Profit Margin, Operating Profit margin, and net profit margin.<\/p>\n\n\n\n<p><strong>Gross Profit Margin<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Gross-Profit-Margin.jpg\" alt=\"Gross Profit Margin\" class=\"wp-image-62688\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Gross-Profit-Margin.jpg 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Gross-Profit-Margin-300x169.jpg 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Gross-Profit-Margin-768x432.jpg 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\"><strong><em>Gross Profit Margin<\/em><\/strong><\/figcaption><\/figure>\n\n\n\n<p>Gross profit margin is defined as the ratio of Gross profit to Revenue. A higher gross profit margin means higher profitability, and consistently high profit margins indicate good financial health.<\/p>\n\n\n\n<p><strong>Operating Profit Margin<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"474\" height=\"109\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Operating-Profit-Margin.jpeg\" alt=\"Operating Profit Margin\" class=\"wp-image-62689\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Operating-Profit-Margin.jpeg 474w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Operating-Profit-Margin-300x69.jpeg 300w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><figcaption class=\"wp-element-caption\"><strong><em>Operating Profit Margin<\/em><\/strong><\/figcaption><\/figure>\n\n\n\n<p>Operating Profit Margin is one of the other famous metrics that gives us an idea of the company&#8217;s profitability. An increasing operating profit margin indicates that the company has been improving its operations yearly.<\/p>\n\n\n\n<p><strong>Net profit margin<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"411\" height=\"79\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Net-profit-margin-formula.png\" alt=\"Net Profit Margin Formula\" class=\"wp-image-62691\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Net-profit-margin-formula.png 411w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Net-profit-margin-formula-300x58.png 300w\" sizes=\"auto, (max-width: 411px) 100vw, 411px\" \/><figcaption class=\"wp-element-caption\"><strong><em>Net Profit Margin Formula<\/em><\/strong><\/figcaption><\/figure>\n\n\n\n<p>Net profit margin gives the investor an idea of how well the company manages its expenses. This financial metric is also used for comparison across industries, as resource utilization can be mentioned in a ratio.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python Code to Calculate Financial Ratios<\/h2>\n\n\n\n<p>We will plot Apple Company&#8217;s gross profit margin in the code below. We have imported Apple as the ticker &#8216;AAPL&#8217; from its Profit and Loss statement, and the same is plotted as a line graph.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n!pip install yfinance matplotlib pandas\nimport yfinance as yf\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\n# Define the stock ticker\nticker = &#039;AAPL&#039;\n\n# Fetch the stock&#039;s financial data\nstock = yf.Ticker(ticker)\nincome_stmt = stock.financials.T\n\n# Keep only the last five years of data\nincome_stmt = income_stmt.tail(5)\n\n# Calculate Gross Profit Margin\nincome_stmt&#x5B;&#039;Gross Profit Margin&#039;] = (income_stmt&#x5B;&#039;Gross Profit&#039;] \/ income_stmt&#x5B;&#039;Total Revenue&#039;]) * 100\n\n# Display the Gross Profit Margin\nprint(&quot;Gross Profit Margin for Apple Inc. (AAPL) over the last five years:&quot;)\nprint(income_stmt&#x5B;&#039;Gross Profit Margin&#039;])\n\n# Plot the Gross Profit Margin\nplt.figure(figsize=(10, 6))\nplt.plot(income_stmt.index, income_stmt&#x5B;&#039;Gross Profit Margin&#039;], marker=&#039;o&#039;, linestyle=&#039;-&#039;, color=&#039;b&#039;)\nplt.title(&#039;Gross Profit Margin of Apple Inc. (AAPL) Over the Last Five Years&#039;)\nplt.xlabel(&#039;Year&#039;)\nplt.ylabel(&#039;Gross Profit Margin (%)&#039;)\nplt.grid(True)\nplt.show()\n\n<\/pre><\/div>\n\n\n<p>Let us look at the output of the above code.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"841\" height=\"547\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Gross-profit-margin-output.png\" alt=\"Gross Profit Margin Output\" class=\"wp-image-62694\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Gross-profit-margin-output.png 841w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Gross-profit-margin-output-300x195.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Gross-profit-margin-output-768x500.png 768w\" sizes=\"auto, (max-width: 841px) 100vw, 841px\" \/><figcaption class=\"wp-element-caption\"><strong><em>Gross Profit Margin Output<\/em><\/strong><\/figcaption><\/figure>\n\n\n\n<p>We can see that Apple&#8217;s Gross profit Margin has been increasing steadily over time. It was below 40% in 2020 and above 40% in the subsequent years. This indicates improving profitability and the company&#8217;s good financial health. Let us now look at the operating profit margin code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport yfinance as yf\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\n# Define the stock ticker\nticker = &#039;AAPL&#039;\n\n# Fetch the stock&#039;s financial data\nstock = yf.Ticker(ticker)\nincome_stmt = stock.financials.T\n\n# Filter data for the last five years\nlast_five_years = income_stmt.index&#x5B;:5]\n\n# Calculate the operating profit margin\nincome_stmt&#x5B;&#039;Operating Profit Margin&#039;] = (income_stmt&#x5B;&#039;Operating Income&#039;] \/ income_stmt&#x5B;&#039;Total Revenue&#039;]) * 100\n\n# Filter the data for the last five years\nop_margin_last_five_years = income_stmt.loc&#x5B;last_five_years, &#039;Operating Profit Margin&#039;]\n\n# Plot the operating profit margin\nplt.figure(figsize=(10, 6))\nplt.plot(op_margin_last_five_years.index, op_margin_last_five_years.values, marker=&#039;o&#039;, linestyle=&#039;-&#039;, color=&#039;b&#039;)\nplt.title(&#039;Operating Profit Margin of Apple Inc. (AAPL) - Last Five Years&#039;)\nplt.xlabel(&#039;Year&#039;)\nplt.ylabel(&#039;Operating Profit Margin (%)&#039;)\nplt.grid(True)\nplt.show()\n\n# Print the operating profit margins\nprint(&quot;Operating Profit Margin for Apple Inc. (AAPL) - Last Five Years&quot;)\nprint(op_margin_last_five_years)\n\n<\/pre><\/div>\n\n\n<p>We have just made some minor tweaks to the above code and plotted the Operating Profit margin. Let us now look at the output of the above code.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"841\" height=\"547\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Operating-Profit-margin-output.png\" alt=\"Operating Profit Margin Output\" class=\"wp-image-62693\" style=\"width:789px;height:auto\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Operating-Profit-margin-output.png 841w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Operating-Profit-margin-output-300x195.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Operating-Profit-margin-output-768x500.png 768w\" sizes=\"auto, (max-width: 841px) 100vw, 841px\" \/><figcaption class=\"wp-element-caption\"><strong><em>Operating Profit Margin Output<\/em><\/strong><\/figcaption><\/figure>\n\n\n\n<p>The operating profit margin increased very suddenly from 2020 to 2021, indicating some positive changes in the company&#8217;s operations. Thereafter, the graph is approximately a straight line, indicating a steady operating profit of around 30%.<\/p>\n\n\n\n<p>Let us now look at the code for the Net Profit margin. It is a small change from the basic code used in the previous two versions.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nimport yfinance as yf\nimport matplotlib.pyplot as plt\n\n# Fetch financial data for Apple (AAPL) from Yahoo Finance\napple = yf.Ticker(&quot;AAPL&quot;)\nincome_statement = apple.financials.T\n\n# Calculate net profit margin\nnet_income = income_statement&#x5B;&#039;Net Income&#039;]\ntotal_revenue = income_statement&#x5B;&#039;Total Revenue&#039;]\nnet_profit_margin = (net_income \/ total_revenue) * 100\n\n# Plot the net profit margin as a line chart\nplt.figure(figsize=(10, 6))\nnet_profit_margin.plot(marker=&#039;o&#039;, color=&#039;b&#039;, linestyle=&#039;-&#039;)\nplt.title(&#039;Net Profit Margin of Apple Inc. (AAPL)&#039;)\nplt.xlabel(&#039;Year&#039;)\nplt.ylabel(&#039;Net Profit Margin (%)&#039;)\nplt.grid(True)\nplt.xticks(rotation=45)\nplt.tight_layout()\nplt.show()\n\n# Display the net profit margin for the last five years\nprint(&quot;Net Profit Margin of Apple Inc. (AAPL) for the Last Five Years:&quot;)\nprint(net_profit_margin.tail(5))\n\n<\/pre><\/div>\n\n\n<p>Let us look into the output of the above code. Essentially, it gives us the exact inference as the Operating profit margin as their graphs are very similar.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"989\" height=\"590\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Net-profit-margin-output.png\" alt=\"Net Profit Margin Output\" class=\"wp-image-62692\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Net-profit-margin-output.png 989w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Net-profit-margin-output-300x179.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/05\/Net-profit-margin-output-768x458.png 768w\" sizes=\"auto, (max-width: 989px) 100vw, 989px\" \/><figcaption class=\"wp-element-caption\"><strong><em>Net Profit Margin Output<\/em><\/strong><\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Here you go! Now you know how to deal with and interpret the ratios provided with the company&#8217;s Profit and loss statement. In this article, we looked into the Net profit margin, Operating Profit Margin, and Gross profit margin. We also saw their implementation and interpretation in the Python programming language.<\/p>\n\n\n\n<p><strong><em>Recommended: <a href=\"https:\/\/www.askpython.com\/python\/examples\/gitpython-git-checkout-commit-id\">Step-by-Step Guide to Checking Out a Commit ID with GitPython<\/a><\/em><\/strong><\/p>\n\n\n\n<p><strong><em>Recommended: <a href=\"https:\/\/www.askpython.com\/python\/examples\/debug-fix-segmentation-faults-python\">How to Debug and Fix Segmentation Faults in Python<\/a><\/em><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the third part of the series, where we compare companies&#8217; financials and evaluate their health. In this article, we will continue discussing how to utilize the Python programming language and its libraries to study different companies&#8217; profit and loss statements. We will also discuss different ratios used for a company&#8217;s financial analysis and [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":63843,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-62657","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/62657","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=62657"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/62657\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/63843"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=62657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=62657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=62657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}