In [1]:
import pandas as pd
'注册日期', '来源渠道', '用户级别', '拨打次数', '客户ID', '客户性别', '年龄', '客户设备', '客户所属省', '客户公司地址', '客户授信状态', '评分原因', '标识原因', '总分', '初审审核说明', '审核人', '备注', '授信额度', '当前授信额度', '可用额度', '授信进件时间', '授信完成时间', '认证 1', '认证 2', '认证 3', '认证 4', '认证 5', '认证 6', '认证 7', '借款合计', '责任员工手机号', '责任员工岗位', '与责任员工的关系级次', '确认状态', '确认用户来源', '确认备注', '营业部经理手机号', '城市经理手机号', '银行卡行', '入司时间', '入司时长(月)', '从业时间', '客户授信入口', '风控分', '芝麻分', '下次是否需要做运营商认证', '已使用额度', '冻结额度', '有没有做过运营商', '是否离职', '是否命中预拉黑名单表', '首次借款时间', '最新一次借款时间', '借款次数', '还款计划总条数', '到期应还次数', '正常未还次数', '正常还款中次数', '正常已还次数', '逾期未还次数', '逾期已还次数', '逾期还款中次数', '逾期1天已还次数', '逾期1_3天次数', '逾期4_7天次数', '逾期8_20天次数', '逾期21天以上次数', '最长逾期天数', '到期应还金额', '标签'
In [4]:
user_info = pd.read_excel('2000_sample.xlsx', 'user_info')
In [105]:
user_info.head()
Out[105]:
In [10]:
# 独立检验
user_info.客户ID.unique().shape
Out[10]:
In [12]:
# 总的分析
user_info.describe()
Out[12]:
有缺失数值的特征: 总分,与责任员工的关系层次 、入司时长 参考分数
In [13]:
SZ = pd.read_excel('2000_sample.xlsx', 'SZ')
In [14]:
SZ.head()
Out[14]:
In [16]:
SZ.user_id.unique().shape
Out[16]:
In [17]:
SZ.shape
# 取同一个用户下的最后一行
Out[17]:
In [18]:
SL_1 = pd.read_excel('2000_sample.xlsx', 'SL-1')
In [19]:
SL_1.head(5)
Out[19]:
In [20]:
SL_2 = pd.read_excel('2000_sample.xlsx', 'SL-2')
In [21]:
SL_2.user_id.unique()
#取最后一条
Out[21]:
In [80]:
SL_2.head()
Out[80]:
In [22]:
SL_3 = pd.read_excel('2000_sample.xlsx', 'SL-3')
In [23]:
SL_3.user_id.unique().shape
Out[23]:
In [100]:
SL_3.head()
Out[100]:
In [25]:
SL_4 = pd.read_excel('2000_sample.xlsx', 'SL-4')
In [45]:
SL_4.head(5)
Out[45]:
In [79]:
SL_5 = pd.read_excel('2000_sample.xlsx', 'SL-5')
SL_5.head(5)
Out[79]:
In [ ]:
In [30]:
BQS_1 = pd.read_excel('2000_sample.xlsx', 'BQS-1')
In [74]:
BQS_1.head()
Out[74]:
In [31]:
BQS_2 = pd.read_excel('2000_sample.xlsx', 'BQS-2')
In [36]:
BQS_2.用户Id.unique().shape
Out[36]:
In [87]:
BQS_2.shape
Out[87]:
In [86]:
BQS_2.head()
Out[86]:
In [37]:
BQS_3 = pd.read_excel('2000_sample.xlsx', 'BQS-3')
In [38]:
BQS_3.用户ID.unique().shape
Out[38]:
In [77]:
BQS_3.head(5)
Out[77]:
In [48]:
from IPython.display import Image
from IPython.core.display import HTML
Image(url= "https://pic3.zhimg.com/80/v2-1919e069b796f92075c128af5eaf7e9a_hd.png")
Out[48]:
In [175]:
# 取最近的一条信息
tmp_1 = SL_1.groupby(by='user_id').apply(lambda x: x[x.ctime==max(x.ctime)])
tmp_2 = SL_4.groupby(by='user_id').apply(lambda x: x[x.ctime==max(x.ctime)])
In [176]:
user_info.columns
Out[176]:
In [177]:
tmp = pd.DataFrame(user_info[['客户ID','是否离职','客户公司地址']]).merge(tmp_1,left_on='客户ID',right_on='user_id',how='left')
tmp = tmp.merge(tmp_2,left_on='客户ID',right_on='user_id',how='left')
tmp.head(5)
Out[177]:
In [178]:
popu_attributes = tmp[['客户ID','是否离职','客户公司地址','省','城市', '地区', '手机运营商', '手机归属省份', '手机归属城市',
'关联身份证数量','身份证户籍城市',
'身份证是否是有效身份证', '身份证户籍省份','身份证户籍地区', '生日','年龄_y', '性别_y']]
# 年龄段划分
popu_attributes.head(5)
Out[178]:
In [179]:
popu_attributes.shape
Out[179]:
In [125]:
# user_info.客户公司地址
#social attributes
In [180]:
# 消费情况
tmp_1 = SL_3.groupby(by = 'user_id',group_keys = False).apply(lambda x: x[x.ctime==max(x.ctime)])
In [181]:
tmp_1.head()
Out[181]:
In [182]:
# 手机品牌 操作系统
tmp_2 = BQS_1.groupby(by='用户ID',group_keys = False).apply(lambda x: x[x.ctime==max(x.ctime)])
In [183]:
tmp_2.dtypes
Out[183]:
In [184]:
tmp = pd.DataFrame(user_info['客户ID']).merge(tmp_1,left_on='客户ID',right_on='user_id',how='left')
#tmp = tmp.merge(tmp_2,left_on='客户ID',right_on='用户ID',how='left')
tmp.head(5)
tmp = tmp.merge(tmp_2,left_on='客户ID',right_on='用户ID',how='left')
tmp.head(5)
Out[184]:
In [185]:
tmp.shape
Out[185]:
In [186]:
consum_attributes = tmp[['客户ID','线上消费分期出现次数', '线下消费分期出现次数', '信用卡代还出现次数',
'小额快速贷出现次数', '线上现金贷出现次数', '线下现金贷出现次数', '其他',
'操作系统','手机运营商家']]
In [156]:
# 芝麻分,客户来源
tmp_1 = user_info[['客户ID','芝麻分','来源渠道']]
value_attributes = tmp_1
value_attributes.shape
In [158]:
lifecycl_attributes = user_info[['客户ID','入司时长(月)','从业时间']]
lifecycl_attributes.head()
Out[158]:
In [260]:
# source
# 取最近的一条信息
tmp_1 = SL_2.groupby(by='user_id').apply(lambda x: x[x.ctime==max(x.ctime)])
tmp_1.head()
Out[260]:
In [261]:
tmp_1.user_id.unique().shape
Out[261]:
In [262]:
tmp_1.user_id.shape
Out[262]:
In [263]:
tmp_2 = BQS_2.groupby(by='用户Id').apply(lambda x: x[x.ctime==max(x.ctime)])
tmp_2.head()
Out[263]:
In [264]:
tmp_2 = tmp_2.drop_duplicates()
In [266]:
tmp_3 = SZ.groupby(by='user_id').apply(lambda x: x[x.ctime==max(x.ctime)])
tmp_3 = tmp_3[['user_id','实名']]
tmp_3.head()
Out[266]:
In [267]:
# 客户授信和评价
tmp_4 = user_info[['客户ID','客户授信状态','标签']]
tmp_4.head()
Out[267]:
In [268]:
# # 绑定不同手机个数和机构数 # 重新构造
# tmp_5 = SL_5.groupby(by='user_id').apply(lambda x: x[x.ctime == max(x.ctime)])
# tmp_5 = tmp_5.drop_duplicates()
# tmp_5.head(5)
In [269]:
tmp = pd.DataFrame(user_info.客户ID).merge(tmp_1, left_on='客户ID', right_on = 'user_id', how='left' )
tmp = tmp.merge(tmp_2,left_on='客户ID',right_on = '用户Id', how='left')
tmp = tmp.merge(tmp_3,left_on='客户ID',right_on = 'user_id', how='left')
tmp = tmp.merge(tmp_4,left_on='客户ID',right_on = '客户ID', how='left')
# tmp = tmp.merge(tmp_5,left_on='客户ID',right_on = 'user_id', how='left')
tmp.head()
Out[269]:
In [270]:
tmp.columns
Out[270]:
In [271]:
tmp.shape
Out[271]:
In [272]:
risk_attributes = tmp[['客户ID','葫芦分','直接联系人', '直接黑人', '间接黑人','认识间接黑人的联系人个数', '认识间接黑人的联系人比例',
'多头借贷次数','身份证三个月关联手机号个数', '手机号六个月星网模型大小', '手机号三个月关联身份证个数',
'实名','客户授信状态', '标签']]
# ,
# '绑定其他手机号码', '此号码绑定其他姓名个数','查询此手机号的机构数']]
In [274]:
risk_attributes.head()
Out[274]:
In [279]:
#在确认数据唯一的情况下,进行左链接
tmp_1 = popu_attributes.merge(consum_attributes,left_on = '客户ID', right_on = '客户ID',how = 'left')
tmp_2 = tmp_1.merge( value_attributes ,left_on = '客户ID', right_on = '客户ID',how = 'left')
tmp_3 = tmp_2.merge(lifecycl_attributes,left_on = '客户ID', right_on = '客户ID',how = 'left')
portrait_attributes = tmp_3.merge( risk_attributes, left_on = '客户ID', right_on = '客户ID',how = 'left')
In [297]:
portrait_attributes.columns
Out[297]:
In [298]:
portrait_attributes = portrait_attributes.rename(columns={'入司时长(月)':'入司时长'})
In [299]:
portrait_attributes.head()
Out[299]:
In [311]:
with open('chuangjin_template1.htm', 'r') as f:
html_string_1 = f.read()
with open('chuangjin_template2.htm', 'r') as f:
html_string_2 = f.read()
with open('chuangjin_template3.htm', 'r') as f:
html_string_3 = f.read()
In [312]:
one_info = portrait_attributes.iloc[1]
In [313]:
portrait_attributes.columns
Out[313]:
In [314]:
html_string_down = html_string_2.format(one_info.客户ID,one_info.身份证是否是有效身份证, '等待处理', one_info.性别_y, one_info.年龄_y,
one_info.生日, one_info.是否离职 , '工作城市等待处理', one_info.客户公司地址, one_info.手机归属省份,
one_info.手机归属城市, one_info.身份证户籍省份 , one_info.身份证户籍城市 , one_info.线上消费分期出现次数, one_info.线下消费分期出现次数,
one_info.信用卡代还出现次数 , one_info.小额快速贷出现次数, one_info.线上现金贷出现次数, one_info.线下现金贷出现次数,one_info.其他,
one_info.操作系统, one_info.手机运营商,one_info.芝麻分, one_info.来源渠道, one_info.入司时长,
one_info.从业时间,one_info.葫芦分, one_info.直接联系人, one_info.直接黑人,one_info.间接黑人,
one_info.认识间接黑人的联系人个数, one_info.认识间接黑人的联系人比例, one_info.多头借贷次数,one_info.身份证三个月关联手机号个数, one_info.手机号六个月星网模型大小,
one_info.手机号三个月关联身份证个数, one_info.实名, one_info.客户授信状态, one_info.标签,'暂未整合',
'暂未整合', '暂未整合'
)
In [316]:
# 拼接成一个
html_string = html_string_1+html_string_down+html_string_3
In [318]:
def save_to_file(file_name, contents):
fh = open(file_name, 'w')
fh.write(contents)
fh.close()
save_to_file('chuangjin-baogao.htm', html_string)