import pandas as pd
3.4.1
long_data15=pd.read_excel('result1_2.xlsx')
long_data15.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 9180 entries, 0 to 9179
Data columns (total 11 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 CustomerId 9180 non-null int64
1 CreditScore 9180 non-null int64
2 Gender 9180 non-null int64
3 Age 9180 non-null int64
4 Tenure 9180 non-null int64
5 Balance 9180 non-null float64
6 NumOfProducts 9180 non-null int64
7 HasCrCard 9180 non-null int64
8 IsActiveMember 9180 non-null int64
9 EstimatedSalary 9180 non-null float64
10 Exited 9180 non-null int64
dtypes: float64(2), int64(9)
memory usage: 789.0 KB
此处对Tenure、Balance列的值进行划分。
long_data16=long_data15.copy()
long_data16.loc[(long_data16.Tenure>6),'Status']='老客户'
long_data16.loc[(long_data16.Tenure<=3),'Status']='稳定客户'
long_data16.loc[(long_data16.Status.isna()),'Status']='新客户'
long_data16.loc[(long_data16.Balance>120000),'AssetStage']='高资产'
long_data16.loc[(long_data16.Balance>90000) & (long_data16.Balance<=120000),'AssetStage']='中上资产'
long_data16.loc[(long_data16.Balance>50000) & (long_data16.Balance<=90000),'AssetStage']='中下资产'
long_data16.loc[(long_data16.Balance<=50000),'AssetStage']='低资产'
long_data17=long_data16.copy()
对文件中的数据进行特征编码,依然采用preprocessing.LabelEncoder将汉字进行编码。
from sklearn.preprocessing import LabelEncoder
t1=long_data16.loc[:,'Status'] #要输入的是标签,不是特征矩阵,所以允许一维数据
t2=long_data16.loc[:,'AssetStage']
le1 = LabelEncoder() #实例化
le1 = le1.fit(t1) # 导入数据
label1 = le1.transform(t1) # transform接口调取结果
long_data16.loc[:,"Status"] = label1
print(long_data16['Status'].unique())
le2 = LabelEncoder() #实例化
le2 = le2.fit(t2) # 导入数据
label2 = le2.transform(t2) # transform接口调取结果
long_data16.loc[:,"AssetStage"] = label2
long_data16['AssetStage'].unique()
[1 2 0]
array([3, 2, 0, 1])
对编码之后的数据保存
long_data16.to_excel("result3.xlsx",encoding = 'openpyxl',index = False)
long_data16.head(5)
CustomerId | CreditScore | Gender | Age | Tenure | Balance | NumOfProducts | HasCrCard | IsActiveMember | EstimatedSalary | Exited | Status | AssetStage | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 15553251 | 713 | 1 | 52 | 0 | 185891.54 | 1 | 1 | 1 | 46369.57 | 1 | 1 | 3 |
1 | 15553256 | 619 | 1 | 41 | 8 | 0.00 | 3 | 1 | 1 | 79866.73 | 1 | 2 | 2 |
2 | 15553283 | 603 | 1 | 42 | 8 | 91611.12 | 1 | 0 | 0 | 144675.30 | 1 | 2 | 0 |
3 | 15553308 | 589 | 1 | 61 | 1 | 0.00 | 1 | 1 | 0 | 61108.56 | 1 | 1 | 2 |
4 | 15553387 | 687 | 1 | 39 | 2 | 0.00 | 3 | 0 | 0 | 188150.60 | 1 | 1 | 2 |
long_data17.head(5)
CustomerId | CreditScore | Gender | Age | Tenure | Balance | NumOfProducts | HasCrCard | IsActiveMember | EstimatedSalary | Exited | Status | AssetStage | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 15553251 | 713 | 1 | 52 | 0 | 185891.54 | 1 | 1 | 1 | 46369.57 | 1 | 稳定客户 | 高资产 |
1 | 15553256 | 619 | 1 | 41 | 8 | 0.00 | 3 | 1 | 1 | 79866.73 | 1 | 老客户 | 低资产 |
2 | 15553283 | 603 | 1 | 42 | 8 | 91611.12 | 1 | 0 | 0 | 144675.30 | 1 | 老客户 | 中上资产 |
3 | 15553308 | 589 | 1 | 61 | 1 | 0.00 | 1 | 1 | 0 | 61108.56 | 1 | 稳定客户 | 低资产 |
4 | 15553387 | 687 | 1 | 39 | 2 | 0.00 | 3 | 0 | 0 | 188150.60 | 1 | 稳定客户 | 低资产 |
3.4.2
long_data18=long_data17.copy()
long_data18=long_data18[long_data18['Exited']==1]
long_data18
CustomerId | CreditScore | Gender | Age | Tenure | Balance | NumOfProducts | HasCrCard | IsActiveMember | EstimatedSalary | Exited | Status | AssetStage | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 15553251 | 713 | 1 | 52 | 0 | 185891.54 | 1 | 1 | 1 | 46369.57 | 1 | 稳定客户 | 高资产 |
1 | 15553256 | 619 | 1 | 41 | 8 | 0.00 | 3 | 1 | 1 | 79866.73 | 1 | 老客户 | 低资产 |
2 | 15553283 | 603 | 1 | 42 | 8 | 91611.12 | 1 | 0 | 0 | 144675.30 | 1 | 老客户 | 中上资产 |
3 | 15553308 | 589 | 1 | 61 | 1 | 0.00 | 1 | 1 | 0 | 61108.56 | 1 | 稳定客户 | 低资产 |
4 | 15553387 | 687 | 1 | 39 | 2 | 0.00 | 3 | 0 | 0 | 188150.60 | 1 | 稳定客户 | 低资产 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
9162 | 15815271 | 755 | 0 | 43 | 6 | 165048.50 | 3 | 1 | 0 | 16929.41 | 1 | 新客户 | 高资产 |
9164 | 15815316 | 644 | 0 | 50 | 9 | 76817.00 | 4 | 1 | 0 | 196371.13 | 1 | 老客户 | 中下资产 |
9168 | 15815490 | 670 | 0 | 40 | 2 | 164948.98 | 3 | 0 | 0 | 177028.00 | 1 | 稳定客户 | 高资产 |
9174 | 15815626 | 640 | 0 | 63 | 2 | 68432.45 | 2 | 1 | 1 | 112503.24 | 1 | 稳定客户 | 中下资产 |
9177 | 15815656 | 541 | 1 | 39 | 9 | 100116.67 | 1 | 1 | 1 | 199808.10 | 1 | 老客户 | 中上资产 |
1837 rows × 13 columns
pd.pivot_table(long_data18,index=['AssetStage','Status'],values=['Exited'],aggfunc='count')
Exited | ||
---|---|---|
AssetStage | Status | |
中上资产 | 新客户 | 151 |
稳定客户 | 181 | |
老客户 | 175 | |
中下资产 | 新客户 | 51 |
稳定客户 | 57 | |
老客户 | 53 | |
低资产 | 新客户 | 152 |
稳定客户 | 180 | |
老客户 | 142 | |
高资产 | 新客户 | 206 |
稳定客户 | 246 | |
老客户 | 243 |
long_data19=pd.pivot_table(long_data18,index=['AssetStage','Status'],values=['Exited'],aggfunc='count')
long_data19.to_csv('新老客户.csv')
long_data19
Exited | ||
---|---|---|
AssetStage | Status | |
中上资产 | 新客户 | 151 |
稳定客户 | 181 | |
老客户 | 175 | |
中下资产 | 新客户 | 51 |
稳定客户 | 57 | |
老客户 | 53 | |
低资产 | 新客户 | 152 |
稳定客户 | 180 | |
老客户 | 142 | |
高资产 | 新客户 | 206 |
稳定客户 | 246 | |
老客户 | 243 |