实训1 处理信用卡数据异常值
1.读取信用卡数据
import numpy as np
import pandas as pd
credit_card = pd.read_csv('D:/学习/Python数据分析与应用PPT、教案、实训数据、习题答案/Python数据分析与应用/第7章/训数据/credit_card.csv',
sep=',',encoding='gbk')
print(credit_card)
测试结果:
信用卡顾客编号 申请书来源 瑕疵户 逾期 呆账 借款余额 退票 拒往记录 强制停卡记录 张数 ... \
0 CDMS0000001 5 2 2 2 2 2 2 2 1 …
1 CDMS0000002 3 2 2 2 2 2 2 2 2 …
2 CDMS0000003 2 2 2 2 2 2 2 2 2 …
3 CDMS0000004 3 2 2 2 2 2 2 2 2 …
4 CDMS0000005 6 2 2 2 2 2 2 2 2 …
… … … … … … … … … … … …
65530 CDMS0065531 4 2 2 2 2 2 2 2 2 …
65531 CDMS0065532 3 2 2 2 2 2 2 2 2 …
65532 CDMS0065533 3 2 2 2 2 2 2 2 2 …
65533 CDMS0065534 6 2 1 1 1 1 1 1 3 …
65534 CDMS0065535 5 2 2 2 2 2 2 2 1 …
个人月收入 个人月开销 住家 家庭月收入 月刷卡额 宗教信仰 人口数 家庭经济 血型 星座
0 1 1 1 1 1 2 4 4 4 10
1 1 2 1 1 2 1 4 4 1 5
2 1 1 1 1 6 6 6 5 1 7
3 1 1 1 1 4 2 4 3 2 6
4 1 3 1 1 5 1 4 3 4 6
… … … … … … … … … … …
65530 4 2 5 4 5 7 6 2 4 6
65531 4 2 5 4 6 3 6 1 4 4
65532 4 2 5 4 3 1 4 1 3 1
65533 4 4 5 4 2 6 1 3 1 12
65534 1 1 1 1 1 2 4 4 4 10
[65535 rows x 28 columns]
2.丢弃逾期,呆账,强制停卡,退票记录,拒往记录为 1 ,瑕疵户为 2 的记录。
import os
index1 = credit_card['逾期'] == 1
index2 = credit_card['呆账'] == 1
index3 = credit_card['强制停卡记录'] == 1
index4 = credit_card['退票'] == 1
index5 = credit_card['拒往记录'] == 1
index6 = credit_card['瑕疵户'] == 2
indexs = index1 & index2 & index3 & index4 & index5 & index6
credit_card1 = credit_card.loc[indexs==False,:]