Skip to content

Instantly share code, notes, and snippets.

@tcramm0nd
Last active October 9, 2022 19:55
Show Gist options
  • Save tcramm0nd/f29bfd63a377249b79f0178919ea2b9f to your computer and use it in GitHub Desktop.
Save tcramm0nd/f29bfd63a377249b79f0178919ea2b9f to your computer and use it in GitHub Desktop.
Create an Amortization Table using Python
import pandas as pd
# Initialize the paramaters of the loan
loan_amount = 18000
apr = 5.29
loan_term = 60
# Get a monthly percentage rate
apr /= 100
mpr = apr / 12
# Calculate the Monthly Payment for a loan
monthly_payment = (loan_amount * mpr)/(1-(1+mpr) ** -loan_term)
def make_payment(principal, mpr, monthly_payment):
'''Makes a 'payment' by subtracting and updated payment amount from the
principal. Returns the principal remaining, and the amount of principal and interest paid
'''
current_interest_payment = principal * mpr
current_principal_payment = monthly_payment - current_interest_payment
principal -= current_principal_payment
return [round(principal, 2), round(current_principal_payment, 2), round(current_interest_payment, 2)]
def main(principal, term_remaining, monthly_payment, mpr):
'''Returns an Amortization Table in the form of a DataFrame
'''
payments = [[principal, 0, 0, 0]]
total_interest = 0
while principal > 0 and term_remaining > 0:
payment = make_payment(principal, mpr, monthly_payment)
principal = payment[0]
term_remaining -= 1
total_interest += payment[2]
payment.append(total_interest)
payments.append(payment)
amortization_table = pd.DataFrame(data=payments,
columns=['Principal Remaining',
'Current Principal Payment',
'Current Interest Payment',
'Total Interest Paid'])
return amortization_table
if __name__ == '__main__':
main(loan_amount, loan_term, monthly_payment, mpr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment