4/5 – Analyze a Balance Sheet with Python

#4 Balance Sheet

A Balance sheet essentially describes the financial position of a company at a specific point in time. Potential investors perform a lot of analysis of the same. It gives them a lot of security if their balance sheet analysis aligns with their long-term investment goals.

In this article, we will learn about the balance sheet, the components of this financial statement, and how we can utilize Python programming language and its libraries to analyze the same.

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

Recommended: Bayesian Inference in Python: A Comprehensive Guide with Examples

Components of Balance Sheet

As mentioned in the previous section, a company’s balance sheet explains its financial position at a specific time. The balance sheet has three components: Assets, equity, and liabilities. The sum of equity and liabilities equals assets, which is known as the Balance sheet equation.

Balance Sheet Equation
Balance Sheet Equation

Let us discuss these components one by one to have better clarity.

Assets

Assets are essentially what the company owns and can provide us with benefits in the future. They are also categorized into Current assets and non-current assets.

Current assets are those that will benefit us during the current business cycle. They include cash and cash equivalents, accounts receivables, inventory, and prepaid expenses.

Non-current assets are those assets that will provide us with gains in the future beyond one business cycle. Some examples of non-current assets are long-term investments, PPE (Property, Plant, and Equipment), etc.

Liabilities

Liabilities are obligations to the company. In simple terms, they are what a company owes to some other person or entity. Similar to assets, liabilities are categorised into current and non-current liabilities.

Current liabilities are those that the company owes during its current business cycle. Some examples are Accounts Payable, Short-term loans, and unearned revenue.

Non-current liabilities are those the company owes after the current business cycle. Some examples are long-term debt, deferred tax, etc.

Equity

Equity is essentially the ownership of the company. It is the leftovers of assets after deducting all liabilities. Examples of equity are common stock and retained earnings.

Let us now look at a sample balance sheet.

Balance Sheet
Balance Sheet

In the above balance sheet, we can see that assets, liabilities, and equities are mentioned. Assets should always match with the sum of liabilities and equity. In accountancy, there’s a famous saying that the left hand ( assets ) should always match the right side ( liabilities & equity ).

Analyzing Balance Sheets with Python

Let us now look at how the Python programming language can be used to study any company’s balance sheet. In the code below, we can find the assets Apple has owned for the last five years. Based on that, we can draw our conclusions.

import yfinance as yf
import matplotlib.pyplot as plt

def get_income_statement(ticker):
    # Fetch income statement data from Yahoo Finance
    stock = yf.Ticker(ticker)
    income_statement = stock.financials.loc['Net Income']
    return income_statement

def get_assets(ticker):
    # Fetch balance sheet data from Yahoo Finance
    stock = yf.Ticker(ticker)
    assets = stock.balance_sheet.loc['Total Assets']
    return assets

def analyze_income_statement(income_statement):
    # Calculate average net income over the last 5 years
    avg_net_income = income_statement.tail(5).mean()
    return avg_net_income

def analyze_assets(assets):
    # Calculate average assets over the last 5 years
    avg_assets = assets.tail(5).mean()
    return avg_assets

def plot_financials(income_statement, assets):
    # Plotting income statement and assets data
    fig, ax1 = plt.subplots(figsize=(12, 6))

    color = 'tab:blue'
    ax1.set_xlabel('Year')
    ax1.set_ylabel('Net Income (in Billions)', color=color)
    income_statement.tail(5).plot(kind='bar', color=color, ax=ax1, position=1, width=0.4)
    ax1.tick_params(axis='y', labelcolor=color)
    ax1.set_xticklabels(income_statement.tail(5).index.strftime('%Y'), rotation=45)

    ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis
    color = 'tab:green'
    ax2.set_ylabel('Total Assets (in Billions)', color=color)  # we already handled the x-label with ax1
    assets.tail(5).plot(kind='bar', color=color, ax=ax2, position=0, width=0.4)
    ax2.tick_params(axis='y', labelcolor=color)

    fig.tight_layout()  # otherwise the right y-label is slightly clipped
    plt.title('Net Income and Total Assets for Apple over the Last 5 Years')
    plt.grid(axis='y', linestyle='--', alpha=0.7)
    plt.show()

if __name__ == "__main__":
    # Fetch income statement and assets data for Apple (AAPL)
    ticker_symbol = "AAPL"
    income_statement = get_income_statement(ticker_symbol)
    assets = get_assets(ticker_symbol)
    
    # Analyze income statement and assets
    avg_net_income = analyze_income_statement(income_statement)
    avg_assets = analyze_assets(assets)
    
    print("Average Net Income for Apple over the last 5 years:", avg_net_income)
    print("Average Total Assets for Apple over the last 5 years:", avg_assets)
    
    # Plot income statement and assets
    plot_financials(income_statement, assets)

From the above code, we can observe that we got income statement and asset data from Yahoo Finance and plotted them.

Let us look at the output of the above code and draw some insights from it.

Assets
Assets over years

Total assets remained constant for three years and then decreased slightly. Moreover, net income also has a decreasing trend, which a long-term investor should account for if they wish to stay invested.

Conclusion

With the Python skills you’ve gained today, you’re well-equipped to tackle financial analysis of balance sheets and beyond. The ability to extract and interpret financial data programmatically opens up new opportunities for deeper insights and more informed investment decisions. How might you apply these techniques to other types of financial analyses or datasets to broaden your analytical prowess?

Recommended: Floyd-Warshall Algorithm in Python: Shortest Path Between Cities

Recommended: Laplace Distribution in Python [with Examples]