Financial Calculations & Valuation Beginner

Loan EMI & Full Amortization Schedule Generator to CSV in Python

Generate a month-by-month loan amortization table in Python Pandas showing EMI, Principal Component, Interest Component, and Closing Balance, exported to CSV.

#pandas#numpy

Problem Statement

Credit analysts and borrowers often need to model prepayment impacts and exact interest outflows across 120 to 240 months. This script builds a complete amortization DataFrame and exports to CSV.

Formula / Core Logic

EMI = P * r * (1 + r)^n / [(1 + r)^n - 1] Monthly Interest = Opening_Balance * r Principal Repaid = EMI - Monthly Interest

Installation

pip install pandas numpy
loan-amortization-schedule-generator-python-csv.py
import pandas as pd
import numpy as np

def generate_amortization_schedule(principal, annual_rate, tenure_months, out_csv='amortization.csv'):
    r = (annual_rate / 100) / 12
    emi = principal * r * ((1 + r) ** tenure_months) / (((1 + r) ** tenure_months) - 1)
    
    balance = principal
    rows = []
    for month in range(1, tenure_months + 1):
        interest = balance * r
        prin_paid = emi - interest
        closing = max(0.0, balance - prin_paid)
        rows.append([month, round(balance, 2), round(emi, 2), round(prin_paid, 2), round(interest, 2), round(closing, 2)])
        balance = closing
        
    df = pd.DataFrame(rows, columns=['Month', 'Opening_Balance', 'EMI', 'Principal_Paid', 'Interest_Paid', 'Closing_Balance'])
    df.to_csv(out_csv, index=False)
    return df

Get the Complete 100+ Python Quant & Automation Bundle

Instant lifetime access to all production-ready scripts, valuation models, and trading bots ($19).

Buy Full Bundle on Gumroad ($19) →

Frequently Asked Questions

Q: How do I model part-prepayments in this schedule?

Subtract the lump-sum prepayment from `closing` in the target month and keep `emi` constant to observe tenure reduction.