所用数据如下
代码文件如下:
具体代码如下:
import pandas as pd
import numpy as np
#生成每月最后一天的时间序列数据
s1=pd.date_range(‘2021-1-31′, freq=’M’,periods=12)
#准备好我们想要得到的报表格式
want=pd.DataFrame(np.zeros((9,12)),index=[“M0″,”M1″,”M2″,”M3″,”M4″,”M5″,”M6″,”M7″,”M7+”],columns=s1)
#导入计划放款数据
data=pd.read_excel(“C:\\Users\\Administrator\\Downloads\\1606266760_885079.xlsx”,skiprows=1,usecols=[4,5,6,7],nrows=12)
#保存当月每期放款的情况
han={}
#从第一个时间2021年1月开始计算每个月的M0-M7 M7+(M8)9种状态对应的金额
j=0
#i表示want数据的第几列
for i in range(want.shape[1]):
#初始化当月每个阶段的金额为0,_0表示当月
M0_0=0
M1_0=0
M2_0=0
M3_0=0
M4_0=0
M5_0=0
M6_0=0
M7_0=0
M8_0=0
print(i,M0_0,M1_0,M2_0,M3_0,M4_0,M5_0,M6_0,M7_0,M8_0,sep=” “)
#调取上个月每个阶段的金额 ,_1表示上一个月
if i==0:
M0_1=0
M1_1=0
M2_1=0
M3_1=0
M4_1=0
M5_1=0
M6_1=0
M7_1=0
M8_1=0
else:
M0_1=want.iloc[0,i-1]
M1_1=want.iloc[1,i-1]
M2_1=want.iloc[2,i-1]
M3_1=want.iloc[3,i-1]
M4_1=want.iloc[4,i-1]
M5_1=want.iloc[5,i-1]
M6_1=want.iloc[6,i-1]
M7_1=want.iloc[7,i-1]
M8_1=want.iloc[8,i-1]
print(i,M0_0,M1_0,M2_0,M3_0,M4_0,M5_0,M6_0,M7_0,M8_0,sep=” “)
#当月放款额状态定为M0
len1=len(han.keys())
#如果当前已有前期放款记录,则对其剩余期数进行调整。如果没有则不用管,比如2020年1月份计算的时候,就不用管。
if len1>0:
for m in han.keys():
if han[m][2]>0:
han[m][2]=han[m][2]-1
else:
han[m][2]=0
#将当期新放款情况记录到库中,剩余未到期数等于放款期数,因为放款当月不用还款。
reportt=data[data[“放款月”]==want.columns[i]].loc[:,[“放款金额”,”期数”]]
for n in reportt.index:
han[n]=[reportt.loc[n,”放款金额”],reportt.loc[n,”期数”],reportt.loc[n,”期数”]]
print(“*******************”,i)
print(han)
M1_0=M0_1*0.0641
M2_0=M1_1*0.78
M3_0=M2_1*0.89
M4_0=M3_1*0.946
M5_0=M4_1*0.97
M6_0=M5_1*0.99
M7_0=M6_1*0.99
M8_0=M7_1*1
M0_0=M1_0*(1-0.78)+M2_0*(1-0.89)+M3_0*(1-0.946)+M4_0*(1-0.97)+M5_0*(1-0.99)+M6_0*(1-0.99)+M7_0*(1-1)+M8_0*(1-1)
for m in han.keys():
M0_0=M0_0+han[m][0]*(han[m][2]/han[m][1])
for k in [0,1,2,3,4,5,6,7,8]:
want.iloc[k,i]=eval(“M”+str(k)+”_0”)
#查看一下结果
want.iloc[:,:3]
#给数据框want增加一行汇总行
want.loc[“汇总”,:]=want.sum(axis=0)