num_cols=['Age', 'Annual_Income', 'Num_of_Loan', 'Num_of_Delayed_Payment', 'Changed_Credit_Limit', 'Amount_invested_monthly', 'Outstanding_Debt' , 'Monthly_Balance']for col in num_cols: df[col]=df[col].apply(keep_num)
Code
df.head()
Customer_ID
Month
Name
Age
Occupation
Annual_Income
Monthly_Inhand_Salary
Num_Bank_Accounts
Num_Credit_Card
Interest_Rate
...
Credit_Mix
Outstanding_Debt
Credit_Utilization_Ratio
Credit_History_Age
Payment_of_Min_Amount
Total_EMI_per_month
Amount_invested_monthly
Payment_Behaviour
Monthly_Balance
Credit_Score
0
CUS_0xd40
January
Aaron Maashoh
23.0
Scientist
19114.12
1824.843333
3
4
3
...
_
809.98
26.822620
22 Years and 1 Months
No
49.574949
80.415295
High_spent_Small_value_payments
312.494089
Good
1
CUS_0xd40
February
Aaron Maashoh
23.0
Scientist
19114.12
NaN
3
4
3
...
Good
809.98
31.944960
NaN
No
49.574949
118.280222
Low_spent_Large_value_payments
284.629162
Good
2
CUS_0xd40
March
Aaron Maashoh
-500.0
Scientist
19114.12
NaN
3
4
3
...
Good
809.98
28.609352
22 Years and 3 Months
No
49.574949
81.699521
Low_spent_Medium_value_payments
331.209863
Good
3
CUS_0xd40
April
Aaron Maashoh
23.0
Scientist
19114.12
NaN
3
4
3
...
Good
809.98
31.377862
22 Years and 4 Months
No
49.574949
199.458074
Low_spent_Small_value_payments
223.451310
Good
4
CUS_0xd40
May
Aaron Maashoh
23.0
Scientist
19114.12
1824.843333
3
4
3
...
Good
809.98
24.797347
22 Years and 5 Months
No
49.574949
41.420153
High_spent_Medium_value_payments
341.489231
Good
5 rows × 26 columns
Split loan type column
Code
loan_t=df['Type_of_Loan'].drop_duplicates().str.split(',',expand=True)loan_type_ls=loan_t[0].drop_duplicates().dropna()for i in loan_type_ls: df[i] = df['Type_of_Loan'].str.contains(i)
Code
# filter negative agedf=df[(df['Age']>18) & (df['Age']<120)]# filter negative num of bank accountdf=df[df['Num_Bank_Accounts']>0]# filter negative num of loandf=df[df['Num_of_Loan']>0]# filter negative num of delayed paymentdf=df[df['Num_of_Delayed_Payment']>0]# filter negative monthly balancedf=df[df['Monthly_Balance']>0]# filter 0 or negative number of credit cardsdf=df[df['Num_Credit_Card']>0]df
Customer_ID
Month
Name
Age
Occupation
Annual_Income
Monthly_Inhand_Salary
Num_Bank_Accounts
Num_Credit_Card
Interest_Rate
...
Credit_Score
Auto Loan
Credit-Builder Loan
Not Specified
Personal Loan
Payday Loan
Mortgage Loan
Home Equity Loan
Debt Consolidation Loan
Student Loan
0
CUS_0xd40
January
Aaron Maashoh
23.0
Scientist
19114.12
1824.843333
3
4
3
...
Good
True
True
False
True
False
False
True
False
False
3
CUS_0xd40
April
Aaron Maashoh
23.0
Scientist
19114.12
NaN
3
4
3
...
Good
True
True
False
True
False
False
True
False
False
5
CUS_0xd40
June
Aaron Maashoh
23.0
Scientist
19114.12
NaN
3
4
3
...
Good
True
True
False
True
False
False
True
False
False
6
CUS_0xd40
July
Aaron Maashoh
23.0
Scientist
19114.12
1824.843333
3
4
3
...
Good
True
True
False
True
False
False
True
False
False
7
CUS_0xd40
August
NaN
23.0
Scientist
19114.12
1824.843333
3
4
3
...
Standard
True
True
False
True
False
False
True
False
False
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
98284
CUS_0xb80d
May
Alex Frewt
32.0
Developer
28796.82
2378.735000
6
6
21
...
Poor
False
False
False
True
True
False
False
False
True
98285
CUS_0xb80d
June
Alex Frewt
32.0
Developer
28796.82
2378.735000
6
6
21
...
Standard
False
False
False
True
True
False
False
False
True
98286
CUS_0xb80d
July
NaN
32.0
Developer
28796.82
2378.735000
6
6
21
...
Poor
False
False
False
True
True
False
False
False
True
98287
CUS_0xb80d
August
Alex Frewt
32.0
Developer
28796.82
NaN
6
6
21
...
Poor
False
False
False
True
True
False
False
False
True
98300
CUS_0x9d41
May
Rothackerz
38.0
Lawyer
41015.55
3152.962500
1194
4
4
...
Standard
False
False
False
False
True
False
False
False
False
63835 rows × 35 columns
Code
# exportdef extract_num(string):returnint(re.sub('[^0-9,]', "", string))def to_months(Yr_mon):try:returnsum([extract_num(s)*12if'Year'in s else extract_num(s) for s in Yr_mon.split('and')])except:return np.nan