numpy 摊销表python中的累计和?

2jcobegt  于 2023-02-23  发布在  Python
关注(0)|答案(1)|浏览(121)

我正在创建一个摊销表,希望有一个利息支付的运行总数,但不断得到关于值是一个浮点数的错误...这是因为利息从来不是一个偶数美元金额是一个整数。我已经尝试了很多方法与cumsum,尝试shift,iloc等,但不断得到错误或没有返回预期的结果。
下面是创建表的代码......我怎样才能成功地添加一个累计利息列?

`import pandas as pd
import numpy as np
import numpy_financial as nf
from datetime import date
import matplotlib.pyplot as plt
%matplotlib inline

interest_rate = 0.36
loan_term = 38/12 #years num of loan rpmt orig / 12
num_payments_per_yr = 12 #rpmt_freq (12, 24, 26)
loan_amt = 7000 #loan_amt
first_payment_date = date(2018, 1, 1) # not needed, changed to be numeric period instead of dates of repayment
adj_interest_rate = interest_rate/num_payments_per_yr
num_periods = loan_term * num_payments_per_yr
co_rate = .2576 / num_periods
llr_rate = .0133       #loan_tb_llr_fct

rng = range(1, int(num_periods)+1)

pmt = round(nf.pmt(adj_interest_rate, num_periods, loan_amt), 2)
pmt

beginning_bal = loan_amt
principal = round(nf.ppmt(pv=loan_amt, rate=adj_interest_rate, nper=num_periods, per=num_periods, fv=0), 2)
interest = nf.ipmt(pv=loan_amt, rate=adj_interest_rate, nper=num_periods, per=num_periods, fv=0)
ending_bal = nf.pv(fv=0, pmt=pmt, rate=adj_interest_rate, nper=0)
records=[]
end_bal = loan_amt
for i in range(1, len(rng)+1):
    bgn_bal = end_bal
    principal = nf.ppmt(pv=loan_amt, rate=adj_interest_rate, nper=num_periods,  per=i, fv=0)
    interest = float(nf.ipmt(pv=loan_amt, rate=adj_interest_rate, nper=num_periods, per=i, fv=0))
    end_bal = nf.pv(fv=0, pmt=pmt, rate=adj_interest_rate, nper=len(rng)-i)
    monthly_co = bgn_bal*co_rate
    npv_rate = 1.2**(i / num_payments_per_yr)
    npvper = -pmt / (npv_rate + co_rate + (.014*(12/num_payments_per_yr))) #20% discount, charge off rate, early payoff rate
    cof = bgn_bal * .0083 #COF basis
    prin_llr = bgn_bal * llr_rate
    int_llr = (-interest * llr_rate)
    net_revenue = (-interest - cof - monthly_co - prin_llr - int_llr)
    records.append((bgn_bal, -pmt, -principal, -interest, end_bal, monthly_co, npvper, npv_rate, cof, prin_llr, int_llr, net_revenue))
    
    columns=['Beginning Balance', 'Payment', 'Principal', 'Interest', 'Ending Balance', 'Monthly Chargeoff', 'NPVPER', 'NPV_Rate', 'COF', 'Prin_LLR', 'Int_LLR', 'Net Revenue']
amort_df = pd.DataFrame.from_records(records, index=rng, columns=columns).round(2)
amort_df`

我试过cumsum,shift,iloc等,我试过谷歌很多方法,但总是得到浮点错误或只是返回当月的利息,而不是前一个月的总和。

brgchamk

brgchamk1#

尝试添加以下代码行:
amort_df['Cumulative_Sum'] = amort_df['Interest'].cumsum()
这将添加一个累计利息列的列。不知道为什么你在使用累计求和时出错,它应该仍然可以使用浮点数。
输出示例:

Beginning Balance  Payment  Principal  ...  Int_LLR  Net Revenue  Cumulative_Sum
1             7000.00   311.22     101.22  ...     2.79         8.55          210.00
2             6898.89   311.22     104.25  ...     2.75         8.43          416.96
3             6794.63   311.22     107.38  ...     2.71         8.30          620.80
4             6687.25   311.22     110.60  ...     2.67         8.17          821.41

相关问题