numpy 如何用最后一个变量填充变量序列直到for循环结束?

siotufzp  于 2023-02-23  发布在  其他
关注(0)|答案(1)|浏览(87)

我正在创建一个摊销表,有30个周期,索引为1-30,问题是特定的贷款还没有到30个周期的末尾,所以我有一个变量可以改变每个周期,我有一个值为17行,然后什么都没有,我如何让我的变量填充到第30行与第17(最后)的值在for循环?

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
cum_int = 0.0
cof = 0.0
rt_npv = 0
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 = loan_amt*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
    rt_npv += npvper
    cof += bgn_bal * .0083 #COF basis
    llr_rate.replace({'llr_rate': {0: np.nan}}).ffill()
    prin_llr = bgn_bal * llr_rate
    int_llr = -interest * llr_rate
    cum_int += interest
    net_revenue = (-cum_int - cof - monthly_co - prin_llr - int_llr)
    records.append((bgn_bal, -pmt, -principal, -interest, -cum_int, end_bal, monthly_co, npvper, rt_npv, npv_rate, cof, prin_llr, int_llr, net_revenue))
uplii1fm

uplii1fm1#

您正在查找ffill()
说明:缺失值将被同一列中最近的可用值替换。因此,将使用同一列中前一行的值来填充空白。

df = df.fillna(method='ffill')

给出以下示例df:

A   B   C
0   1   5   10
1   2   nan 11
2   nan nan 12
3   4   8   nan
4   nan 9   14

结果:

A   B   C
0   1   5   10
1   2   8   11
2   2   8   12
3   4   8   12
4   2   9   14

相关问题