Skip to content

Instantly share code, notes, and snippets.

@mGalarnyk
Created December 7, 2018 19:03
Show Gist options
  • Save mGalarnyk/82a1d38f9ddf6ee91b186de6f0c106ae to your computer and use it in GitHub Desktop.
Save mGalarnyk/82a1d38f9ddf6ee91b186de6f0c106ae to your computer and use it in GitHub Desktop.
Calculate Car loan table. How much of a monthly payment (EMI) go to interest and how much go to principal
P = 34689.96
term = 60
def generate_loan_table(P, term, interest_rate=0.0702):
def calc_emi(P, n, interest_rate):
r = interest_rate / 12
numerator = (r *((1 + r)**(n)) )
denominator = ((1 + r)**(n)) - 1
emi = P * (numerator / denominator)
emi = np.round(emi, 2)
return(emi)
def calc_interest(P, emi, interest_rate):
i_paid = np.floor(((interest_rate/12)*P)*100)/100
p_paid = np.round(emi - i_paid, 2)
new_p = np.round(P - p_paid,2)
return(emi, i_paid, p_paid, new_p)
emi = calc_emi(P, term, interest_rate)
payment_list = []
for n in range(1, term + 1):
emi,i_paid,p_paid, new_p = calc_interest(P, emi, interest_rate)
payment_list.append([n, P,emi, i_paid, p_paid, new_p])
P = np.round(new_p,2)
payment_table = pd.DataFrame(payment_list, columns = ['Month',
'Starting Balance',
'Repayment',
'Interest Paid',
'Principal Paid',
'New Balance'])
return(payment_table, np.round(payment_table['Interest Paid'].sum(), 2), emi)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment