Analysing Balance Sheet Financial Ratios with Python and pandas
Balance sheet ratios turn a page of line items into a few comparable numbers: can the company pay its short-term bills (liquidity), and how much of it is financed with debt (leverage)? With pandas, you load the balance sheet with one column per year, transpose it so each line item becomes a column, and compute every ratio for every year in a single vectorised expression.
The figures below describe a fictional company and are only there to make the arithmetic visible; they are not drawn from any real filing. The code was run with pandas 3.0, but it uses nothing that older pandas versions lack.
The data: one row per line item, one column per year
Financial statements are usually published in "wide" form, with years as columns. Save yours as a CSV in the same shape (amounts in thousands, say):
item,2024,2025
cash,120,95
receivables,210,260
inventory,180,240
other_current_assets,40,35
total_current_assets,550,630
total_assets,1500,1640
accounts_payable,150,190
short_term_debt,90,140
other_current_liabilities,60,70
total_current_liabilities,300,400
long_term_debt,400,420
total_liabilities,700,820
total_equity,800,820
Real statements use inconsistent labels ("Cash and cash equivalents", "Trade receivables"…). Map them to short, stable snake_case names first; every formula below depends on those names. If the export comes from a spreadsheet in a regional encoding and read_csv fails, see how to fix UnicodeDecodeError when reading a CSV in pandas.
Load it and check that it balances
The accounting identity assets = liabilities + equity must hold for every year. Testing it catches typos, missing rows and sign errors before they reach a ratio.
import pandas as pd
bs = pd.read_csv("balance_sheet.csv", index_col="item")
print(bs.loc[["total_assets", "total_liabilities", "total_equity"]])
check = bs.loc["total_assets"] - bs.loc["total_liabilities"] - bs.loc["total_equity"]
assert (check == 0).all(), check
2024 2025
item
total_assets 1500 1640
total_liabilities 700 820
total_equity 800 820
With real, rounded figures, compare with a tolerance instead (check.abs().lt(0.5).all()), and check that subtotals equal the sum of their components.
Compute the ratios for every year at once
Transposing with .T gives one row per year and one column per line item, so each ratio becomes a column expression that pandas evaluates for all years together.
b = bs.T # one row per year, one column per line item
ratios = pd.DataFrame({
"current_ratio": b.total_current_assets / b.total_current_liabilities,
"quick_ratio": (b.cash + b.receivables) / b.total_current_liabilities,
"cash_ratio": b.cash / b.total_current_liabilities,
"debt_to_equity": (b.short_term_debt + b.long_term_debt) / b.total_equity,
"liabilities_to_assets": b.total_liabilities / b.total_assets,
"equity_ratio": b.total_equity / b.total_assets,
"working_capital": b.total_current_assets - b.total_current_liabilities,
})
print(ratios.T.round(2).assign(change=lambda d: d["2025"] - d["2024"]))
2024 2025 change
current_ratio 1.83 1.58 -0.25
quick_ratio 1.10 0.89 -0.21
cash_ratio 0.40 0.24 -0.16
debt_to_equity 0.61 0.68 0.07
liabilities_to_assets 0.47 0.50 0.03
equity_ratio 0.53 0.50 -0.03
working_capital 250.00 230.00 -20.00
Transposing back for display puts ratios in rows and years in columns, and assign adds the year-on-year change.
What each ratio tells you
| Ratio | Formula | Question it answers |
|---|---|---|
| Current ratio | current assets / current liabilities | Do assets that turn into cash within a year cover obligations due within a year? |
| Quick ratio | (cash + receivables) / current liabilities | Same question, without counting on selling inventory |
| Cash ratio | cash / current liabilities | How much could be paid today? |
| Debt to equity | interest-bearing debt / equity | How much borrowing per unit of owners' funds? |
| Liabilities to assets | total liabilities / total assets | What share of assets is financed by creditors? |
| Equity ratio | equity / total assets | The complement: the share financed by owners |
Definitions vary. Some analysts include marketable securities in the quick ratio, or use total liabilities in the numerator of debt to equity. Write your definition next to the code and keep it constant across years and companies.
Reading this example
The fictional company is still liquid by the current ratio (1.58), but the trend points one way: receivables and inventory grew faster than cash, short-term debt rose from 90 to 140, and the quick ratio dropped below 1. In plain words, it now needs to sell inventory to meet its short-term obligations. Leverage edged up too. None of this is alarming on its own, but each figure is a prompt for a question to ask, about collection times, stock levels and refinancing plans.
Common-size balance sheet
Dividing every line by total assets removes the effect of size, so you can compare years or companies of different scale. DataFrame.div with a row as the divisor does it in one line:
common = bs.div(bs.loc["total_assets"]) * 100
print(common.loc[["cash", "receivables", "inventory", "total_current_assets",
"total_liabilities", "total_equity"]].round(1))
2024 2025
item
cash 8.0 5.8
receivables 14.0 15.9
inventory 12.0 14.6
total_current_assets 36.7 38.4
total_liabilities 46.7 50.0
total_equity 53.3 50.0
Cash fell from 8% to under 6% of assets while inventory rose, the same story the ratios told, now in proportions.
Simple automated flags
Once ratios are columns, rules are a loop. Thresholds depend on the industry: a retailer with fast-moving stock can live with a low quick ratio that would worry you at a software company. Treat the thresholds below as placeholders to tune.
rules = {"current_ratio": (1.0, "below 1: current liabilities exceed current assets"),
"quick_ratio": (1.0, "below 1: depends on selling inventory to pay short-term bills")}
latest = ratios.iloc[-1]
for name, (floor, msg) in rules.items():
if latest[name] < floor:
print(f"{name} = {latest[name]:.2f}: {msg}")
quick_ratio = 0.89: below 1: depends on selling inventory to pay short-term bills
Scaling up to many companies
For a portfolio, stack the statements in long format with columns company, year, item and value, then pivot with df.pivot_table(index=["company", "year"], columns="item", values="value"). The ratio code above works unchanged on that table, and pandas groupby gives you per-company medians or year-on-year changes with groupby("company").diff(). If large totals print in scientific notation, switch it off in the display options.
Limits of balance sheet ratios
- A balance sheet is a snapshot. It shows one date, often a quiet one at year end. Seasonal businesses can look very different mid-year.
- Accounting choices matter. Lease accounting, inventory valuation and the classification of debt change the ratios without changing the business.
- Ratios need context. Compare with the same company's history and with peers in the same industry, not with universal rules of thumb.
Summary
Keep the balance sheet with stable item names, assert that assets equal liabilities plus equity, transpose so line items become columns, and compute each ratio as a column expression. Add year-on-year changes, a common-size view with div, and threshold flags tuned to the industry, then read the numbers as questions rather than verdicts.
Sources
- pandas documentation, pandas.DataFrame.div.
- U.S. Securities and Exchange Commission, Beginners' Guide to Financial Statements.
- IFRS Foundation, IAS 1 Presentation of Financial Statements, and its successor IFRS 18 Presentation and Disclosure in Financial Statements.
Datapandas groupby
Split, apply, combine: totals per group, named aggregation, transform, filter and the defaults that changed in pandas 3.
DataSuppress scientific notation
Stop pandas and NumPy printing floats like 1.2e+09: set_printoptions(suppress=True), display.float_format, option_context, Styler and to_csv.
DataUnicodeDecodeError in read_csv
Why pd.read_csv raises UnicodeDecodeError: 'utf-8' codec can't decode byte, and how to fix it with the right encoding, BOM handling and UTF-8 conversion.