使用Pandas进行变量衍生
? 上圖是一個(gè)記錄客戶(hù)近10期的逾期記錄的數(shù)據(jù)框,基于逾期行為數(shù)據(jù)進(jìn)行變量的衍生。比如cust1客戶(hù),最近1期狀態(tài)為M0,最近2期狀態(tài)為M1,還款歷史字段應(yīng)為“0001110210”。
1.計(jì)算近p期總逾期次數(shù)
方法一:
方法二:
def delqcnt(x,term):result=0 j=term+1for i in range(1,j): result=result+(1 if (x['delq'+str(i)]>0) else 0) #計(jì)算每期記錄是否逾期return result #輸出加總逾期次數(shù)結(jié)果 df1['加總逾期次數(shù)']=df1.apply(lambda x:delqcnt(x,10),axis=1)結(jié)果:
2.計(jì)算近p期最大的連續(xù)逾期次數(shù)
def maxcontinue(x,term):result=0 temp=0 #臨時(shí)存儲(chǔ)的變量j=term+1 for i in range(1,j): if (x['delq'+str(i)]==0) :temp=0 #如果當(dāng)前字段不逾期,重新計(jì)算連續(xù)次數(shù)temp=temp+(1 if (x['delq'+str(i)]>0) else 0) #計(jì)算連續(xù)逾期次數(shù)result=max(result,temp) #每一段連續(xù)逾期比較,取最大的一段逾期return result #輸出最大的逾期次數(shù) df1['最大的連續(xù)逾期次數(shù)']=df1.apply(lambda x:maxcontinue(x,10),axis=1)即計(jì)算還款歷史中連續(xù)大于0的個(gè)數(shù),cust1客戶(hù)最大連續(xù)逾期次數(shù)為3,為1->1->1,cust3連續(xù)逾期次數(shù)為5,為1->2->3->4->5。
3.最近一次逾期距今月份數(shù)
方法一:
方法二:
def lastdelq_(df1,term):auto_value=[]for i in range(len(df1)):row_value=df1.ix[i,'delq1':'delq'+str(term)]if row_value.max()<=0:indexs=0auto_value.append(indexs)else:indexs=1for j in row_value:if j>0:breakindexs+=1auto_value.append(indexs)return auto_value df1['最近一次逾期距今月份數(shù)_2']=lastdelq_(df1,10)4.計(jì)算逾期連續(xù)增加的次數(shù)
def maxcontinuechg(x,term):result=0 #輸出結(jié)果默認(rèn)為0temp=0 #臨時(shí)變量設(shè)置為0for i in range(1,term): #循環(huán)取值j=i+1 #取后一位期數(shù)的值if (x['delq'+str(j)]<=x['delq'+str(i)]):temp=0 #若逾期期數(shù)沒(méi)有增加,設(shè)置臨時(shí)變量為0temp=temp+(1 if (x['delq'+str(j)]>x['delq'+str(i)]) else 0) #計(jì)算逾期連續(xù)增加的次數(shù)result=max(result,temp) #若有多段連續(xù)逾期,比對(duì)取最大的一段return result #輸出逾期連續(xù)增加的次數(shù) df1['逾期連續(xù)增加的次數(shù)']=df1.apply(lambda x:maxcontinuechg(x,10),axis=1)5.計(jì)算逾期增加的次數(shù)
def maxcontinuechg(x,term):result=0 #輸出結(jié)果默認(rèn)為0temp=0 #臨時(shí)變量設(shè)置為0for i in range(1,term): #循環(huán)取值j=i+1 #取后一位期數(shù)的值temp=temp+(1 if (x['delq'+str(j)]>x['delq'+str(i)]) else 0) #計(jì)算逾期連續(xù)增加的次數(shù)result=max(result,temp) #若有多段連續(xù)逾期,比對(duì)取最大的一段return result #輸出逾期增加的次數(shù) df1['逾期增加的次數(shù)']=df1.apply(lambda x:maxcontinuechg(x,10),axis=1)6.計(jì)算最近p個(gè)月,每?jī)蓚€(gè)月間的增長(zhǎng)量的最大值
def maxtwomonth_interval(x,term):result=0for i in range(1,term):j=i+1temp=x[j]-x[i]# print(temp)result=max(result,temp)return result df1['每?jī)蓚€(gè)月間的增長(zhǎng)量的最大值']=df1.apply(lambda x:maxtwomonth_interval(x,9),axis=1)7.計(jì)算最近p個(gè)月取最大值的月份距現(xiàn)在的月份數(shù)
def maxmonth_interval(x,term):df=x.ix[:,'delq1':'delq'+str(term)]df['_max'] = np.nanmax(df, axis=1)for i in range(1, term + 1):df['delq' + str(i)] = list(df['delq' + str(i)] == df['_max'])df_value = np.where(df == True, 1, 0)auto_value = []for i in range(len(df_value)):row_value = df_value[i, :]indexs = 1for j in row_value:if j == 1:breakindexs += 1auto_value.append(indexs)return auto_value df1['最近p個(gè)月取最大值的月份距現(xiàn)在的月份數(shù)']=maxmonth_interval(df1,10)8.計(jì)算最近p個(gè)月的均值
def Avg(x,p):df=x.ix[:,'delq1':'delq'+str(p)]auto_value=np.nanmean(df,axis=1)return auto_value df1['最近p個(gè)月的均值']=Avg(df1,10)計(jì)算最近p個(gè)月的和
def Tot(x,p):df=x.ix[:,'delq1':'delq'+str(p)]auto_value=np.nansum(df,axis=1)return auto_value df1['最近p個(gè)月的求和']=Tot(df1,10)計(jì)算最近p個(gè)月特征的最大值
def Max(x,p):df=x.ix[:,'delq1':'delq'+str(p)]auto_value=np.nanmax(df,axis=1)return auto_value df1['最近p個(gè)月的最大值']=Max(df1,10)【作者】:Labryant
【原創(chuàng)公眾號(hào)】:風(fēng)控獵人
【簡(jiǎn)介】:某創(chuàng)業(yè)公司策略分析師,積極上進(jìn),努力提升。乾坤未定,你我都是黑馬。
【轉(zhuǎn)載說(shuō)明】:轉(zhuǎn)載請(qǐng)說(shuō)明出處,謝謝合作!~
總結(jié)
以上是生活随笔為你收集整理的使用Pandas进行变量衍生的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 一文了解助贷业务
- 下一篇: 使用pandas处理时间变量