【对比Python】逆分组
任务:列出分期贷款明细,当期还款额、当期利息、当期本金、剩余本金。
Python
1 | import numpy as np |
2 | import pandas as pd |
3 | loan_data = pd.read_csv('E:\\txt\\loan.csv',sep='\t') |
4 | loan_data['mrate'] = loan_data['Rate']/(100*12) |
5 | loan_data['mpayment'] = loan_data['LoanAmt']*loan_data['mrate']*np.power(1+loan_data['mrate'],loan_data['Term']) \ |
6 | /(np.power(1+loan_data['mrate'],loan_data['Term'])-1) |
7 | loan_term_list = [] |
8 | for i in range(len(loan_data)): |
9 | tm = loan_data.loc[i]['Term'] |
10 | loanid = np.tile(loan_data.loc[i]['LoanID'],tm) |
11 | loanamt = np.tile(loan_data.loc[i]['LoanAmt'],tm) |
12 | term = np.arange(1,tm+1) |
13 | rate = np.tile(loan_data.loc[i]['mrate'],tm) |
14 | payment = np.tile(np.array(loan_data.loc[i]['mpayment']),loan_data.loc[i]['Term']) |
15 | interest = np.zeros(len(loanamt)) |
16 | principal = np.zeros(len(loanamt)) |
17 | principalbalance = np.zeros(len(loanamt)) |
18 | loan_amt = loanamt[0] |
19 | for j in range(len(loanamt)): |
20 | interest[j] = loan_amt*loan_data.loc[i]['mrate'] |
21 | principal[j] = payment[j] - interest[j] |
22 | principalbalance[j] = loan_amt - principal[j] |
23 | loan_amt = principalbalance[j] |
24 | loan_data_df = pd.DataFrame(np.transpose(np.array([loanid,loanamt,term,rate,payment,interest,principal,principalbalance])), columns = ['loanid','loanamt','term','mRate','payment','interest','principal','principalbalance']) |
25 | |
loan_term_list.append(loan_data_df) | |
26 | loan_term_pay = pd.concat(loan_term_list,ignore_index=True) |
27 | print(loan_term_pay) |
Padans处理此类逆分组的问题就显得非常麻烦。
集算器
A | ||
1 | E:\\txt\\loan.csv | |
2 | =file(A1).import@t() | |
3 | =A2.derive(Rate/100/12:mRate,LoanAmt*mRate*power((1+mRate),Term)/(power((1+mRate),Term)-1):mPayment) | |
4 | =A3.news((t=LoanAmt,Term);LoanID, LoanAmt, mPayment:payment, to(Term)(#):Term, mRate, t* mRate:interest, payment-interest:principal, t=t-principal:principlebalance) |
利用逆分组的函数,很容易的搞定逆分组类型的问题。