2/5 – Comparing Financial Performance of Companies with Python – P&L Statement

#2 P&L Statement

One of the most critical financial statements is the profit and loss statement. This statement gives us the Revenue earned, Costs incurred, and profit or loss generated. We will also be looking at the company’s EPS, which gives us some insights into its earning capability.

In this article, we will learn how to utilize libraries like Pandas in Python to extract various insights from the Profit and loss statement.

Recommended: Survival Analysis in Python: A Comprehensive Guide with Examples

Breaking Down the P&L Statement

Revenue: Revenue is the earnings the company generates from its normal business operations by selling products or services. Steel Authority of India Ltd. (SAIL) is the revenue from stainless steel it manufactures and sells.

Expense: Expense is the opposite of revenue in some sense. It is the cost incurred by selling a product or service. For example, SAIL has expenses such as raw materials, distribution charges, etc.

Profit Before Tax (PBT): Profit is defined as Expenses subtracted from Revenue. PBT is the same: the average profit from the company’s everyday operations.

Profit After Tax (PAT): Profit After Tax is PBT, from which the tax portion is subtracted. For example, the profit is Rs 100 (PBT), the interest is Rs 20, and PAT is { Rs 100 – Rs 20 }. Every country has different taxes, and this component is not uniform across all regions. This is also referred to as Net Income.

Earnings per Share (EPS): Earnings Per Share (EPS) represents a company’s profitability on a per-share basis, offering a direct insight into financial health. They are calculated by dividing Net Income by outstanding shares issued by the company.

Since we are familiar with the components, let us now look at what a P&L statement looks like.

PL Statement
P&L Statement

This is the general outlook of a P&L statement. Let us now look at how to implement the Python programming language in calculating the different components of P&L statements.

Analyzing P&L Statements with Python

Let’s calculate Revenue and plot it using Apple’s Python programming language. We will use Yahoo Finance for this.

!pip install yfinance matplotlib
import yfinance as yf
import matplotlib.pyplot as plt

def get_revenue(ticker):
    # Fetch income statement data from Yahoo Finance
    stock = yf.Ticker(ticker)
    revenue = stock.financials.loc['Total Revenue']
    return revenue

def plot_revenue(revenue):
    # Plotting revenue data
    years = revenue.tail(5).index.year
    revenue_values = revenue.tail(5).values / 1e9  # Convert to billions

    plt.figure(figsize=(10, 6))
    plt.bar(years, revenue_values, color='skyblue')
    plt.title('Total Revenue for Apple over the Last 5 Years')
    plt.ylabel('Total Revenue (in Billions)')
    plt.grid(axis='y', linestyle='--', alpha=0.7)

if __name__ == "__main__":
    # Fetch revenue data for Apple (AAPL)
    ticker_symbol = "AAPL"
    revenue = get_revenue(ticker_symbol)
    # Plot revenue

We have imported the necessary modules from Python to extract data from Yahoo Finance. We also plot revenue data for the last five years. We use the ticker ‘AAPL’ to import Apple data from Yahoo Finance.

Revenue Of Apple
Visualizing Apple’s Revenue Growth

Similarly, we can also examine last year’s profits. We will import pandas and Yahoo Finance, then import data from Apple as a ticker, ‘AAPL.’

!pip install yfinance matplotlib pandas
import yfinance as yf
import matplotlib.pyplot as plt
import pandas as pd

# Fetch data for Apple
ticker = 'AAPL'
apple = yf.Ticker(ticker)

import yfinance as yf
import matplotlib.pyplot as plt
import pandas as pd

# Fetch data for Apple
ticker = 'AAPL'
apple = yf.Ticker(ticker)

# Extract financial data
income_statement = apple.financials.T  # Transpose to have years as rows

# Extract the last 5 years' profits (net income)
profits = income_statement['Net Income'].iloc[:5]

# Prepare data for plotting
years = profits.index.strftime('%Y')
profits = profits.values

# Plotting the data
plt.figure(figsize=(10, 6))
plt.bar(years, profits, color='blue')

plt.ylabel('Net Income (USD)')
plt.title('Apple Net Income (Profits) over the Last 5 Years')

Let’s look at the output of the above code. We extract the profits of the last five years and then plot the data.

Apple Profits
Charting Apple’s Net Income

Now you know about how to extract the profits from Yahoo Finance. Similarly, you can extract other data as well.


You’ve now navigated through the essentials of analyzing P&L statements with Python, equipping yourself with the analytical tools needed to uncover financial insights. As you continue to apply these skills, consider how they might transform your approach to economic analysis. What potential impacts could enhanced financial visibility have on your strategic decisions?

Recommended: (1/5) Understanding Technical Analysis and Indicators using Python

Recommended: (2/5) Moving Average Crossover Strategy: Python Implementation