Articles

How do you compute a weighted average interest rate in pandas?

Multiply the rate by the amount, sum both by group, divide. One line that mean() does not replace.

The average rate of a loan book is weighted by the amount lent: a $3 million loan counts ten times more than a $300,000 one. In pandas, add a column rate x principal, sum it and the principal by group, and divide. df["rate"].mean() gives every loan the same weight. On the fictional book of this example, the weighted rate is 7.94% against a simple average of 7.85%.

The code

def by_sector():
    df = merged()
    df = df.assign(rate_x_principal=df["rate"] * df["principal"])
    g = df.groupby("sector").agg(loans=("loan_id", "count"),
                                 principal=("principal", "sum"),
                                 rxp=("rate_x_principal", "sum"))
    g["rate"] = g["rxp"] / g["principal"]          # weighted by principal
    return g.drop(columns="rxp").sort_values("principal", ascending=False)

The product is summed within each group before the division. Dividing first, or averaging the rates of the groups, gives a different and wrong number.

The result by sector

Calder Lane Credit (fictional): 60 loans, $94,165,000.
SectorLoansPrincipalWeighted rateSimple average
Logistics12$23,635,0007.95%7.77%
Manufacturing11$22,015,0008.11%8.12%
Technology14$20,545,0008.01%7.88%
Healthcare9$12,880,0007.88%7.91%
Hospitality7$8,430,0007.37%7.38%
Retail7$6,660,0007.99%7.89%

The largest gap is in Logistics: 7.95% weighted against 7.77% simple. The average of the six sector rates, 7.88%, is a third answer and matches neither: a total is computed from the rows, never from the subtotals.

Which weight?

Original principal describes what was lent; the outstanding balance describes what earns interest today. For the coupon of a book at a reporting date, weight by the balance of the performing loans: on the same book that gives 7.93%. State the weight in the label.

Sources

  1. Julian R. Sterling, Python & pandas for Finance (course, 2026): running case Calder Lane Credit, fictional
  2. pandas documentation: merge (validate, indicator), groupby, cut, read_excel (consulted November 2026)
  3. Basel Committee on Banking Supervision, Guidelines on prudent treatment of problem assets (2017)

Calder Lane Credit and its loans are fictional; the data were generated by code. Educational material, not investment advice. Prepared with AI assistance.

Learn Python and pandas on a real loan book

31 lessons from zero: import, clean and join the files of a finance team, build payment schedules, age arrears, compare vintages, measure yields, draw charts, write the Excel report and reconcile it, on a 60-loan portfolio. Start and solution files, notebooks, exercises and a certificate exam ($199).

Read module 1 free →

FAQ

How do you calculate a weighted average in pandas?

Multiply the values by the weights in a new column, sum it and the weights (by group with groupby if needed), and divide the first sum by the second.

Why not use mean() for interest rates?

mean() gives every loan the same weight. A portfolio rate must weight each loan by its amount, or large and small loans count the same.

Should the weight be the original principal or the balance?

The balance outstanding for what the book earns today; the original principal for what was originated. Say which one you used.

Related