-
Notifications
You must be signed in to change notification settings - Fork 0
/
Credit card Project.py
659 lines (315 loc) · 11.7 KB
/
Credit card Project.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="darkgrid")
pd.options.display.max_columns=50
# In[2]:
df= pd.read_csv('Credit card transactions - India - Simple.csv')
# In[3]:
df.head()
# In[4]:
df.describe(percentiles=[0.75,0.8,0.90,0.95,0.99])
# In[5]:
df.info()
# In[6]:
df.isnull().sum()
# ### There are no missing values in our dataset.
# ## Data Cleaning
# In[7]:
df.Date=pd.to_datetime(df['Date'])
# In[8]:
df.info()
# In[9]:
import datetime as dt
# In[10]:
month = df['Date'].dt.month_name()
# In[11]:
df["Day"] = df['Date'].dt.day_name()
# In[12]:
df["Weekday"]= df['Date'].dt.weekday
# In[13]:
year= df.Date.dt.year
# In[14]:
df=pd.concat([df,month,year],axis=1)
# In[15]:
df.head()
# In[16]:
df.columns= ["index","city","Date","Card Type","Exp Type","Gender","Amount","Day","Weekday","Month","Year"]
# In[17]:
df.head()
# In[18]:
df.drop(["index","Date"], axis=1, inplace= True)
# In[19]:
df.head()
# In[20]:
df[["City","Country"]]=df.city.str.split(",",expand=True)
# In[21]:
df.head()
# In[22]:
df.drop(['city','Country'], axis = 1, inplace=True)
# In[23]:
df.head()
# In[24]:
df["Card Type"].value_counts()
# In[25]:
df['Exp Type'].value_counts()
# In[26]:
df.Gender.value_counts()
# In[27]:
df.Year.value_counts()
# In[28]:
df.Month.value_counts()
# ## Identifying and Handling outliers
# In[29]:
df.Amount.describe()
# In[30]:
df[df.Amount>385000]
# In[31]:
sns.boxplot(df.Amount)
plt.show()
# There are no outliers in our Data
# ## Univariate Analysis
# In[32]:
x = ["Silver","Signature","Platinum","Gold"]
y= df["Card Type"].value_counts()
print (y)
plt.figure(figsize=(6,6))
plt.pie(y, labels=x, shadow=True,autopct= "%0.0f%%",textprops={"fontsize":20}, wedgeprops={"width":0.6},explode=(0.1,0,0.1,0))
plt.legend(loc="center right", bbox_to_anchor=(1.3,0.6));
# In[33]:
print(df["Exp Type"].value_counts())
plt.figure(figsize=(9,9))
sns.countplot(x="Exp Type",hue="Exp Type",data= df,edgecolor="black")
plt.legend(loc="center right" ,bbox_to_anchor=(1.3,0.6));
# In[34]:
df.head()
# In[35]:
x=["Female","Male"]
y=df.Gender.value_counts()
print(y)
plt.figure(figsize=(8,8))
plt.pie(y, labels=x,shadow=True,autopct="%0.0f%%",textprops={"fontsize":20,"color":"black"},colors=("pink","blue"),wedgeprops={"width":0.7})
plt.legend(loc="center right",bbox_to_anchor=(1.3,0.8));
# In[36]:
x=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
y=df.Day.value_counts()
plt.figure(figsize=(6,6))
sns.countplot(x="Day",data=df,edgecolor="black")
plt.xticks(rotation=45)
# In[37]:
df.Month.value_counts()
plt.figure(figsize=(6,6))
sns.countplot(x="Month",data=df,edgecolor="black")
plt.xticks(rotation=(90))
# In[38]:
df.City.value_counts().head(10).to_frame()
# In[39]:
df["Amount"].agg(["sum","min","max","mean"]).reset_index()
# # Bivariate Analysis
# In[40]:
df.sample(1)
# ### Checking for corerlation
# In[41]:
sns.heatmap(df.corr(),annot=True,cmap="coolwarm")
# There is no strong corelation
# In[42]:
df.plot.scatter(x="Gender",y="Amount")
# In[43]:
plt.scatter(df.Gender,df.Amount)
# In[44]:
sns.pairplot(data=df,vars=["Amount","Weekday","Year"])
plt.show()
# In[45]:
df[["Weekday","Year","Amount"]].corr()
# In[46]:
### Which card type were mostly used by the Males and Females
df.groupby("Gender")["Card Type"].value_counts().to_frame()
# In[47]:
plt.figure(figsize=(8,8))
sns.countplot(x="Gender",hue="Card Type",data=df,edgecolor="black")
#plt.figure(figsize=(8,8))
#sns.countplot(x="Card Type",hue="Gender",data=df,palette=["pink","blue"],edgecolor="black");
# In[48]:
print(df.groupby(['Gender'])['Card Type'].value_counts())
plt.figure(figsize=(8,8))
sns.countplot(x="Gender",hue="Card Type",data=df,edgecolor='black')
plt.legend(loc='center right',bbox_to_anchor=(1.2,0.7))
# Females have used Silver card more whereas Males have used Platinum card more.
# In[ ]:
# In[49]:
print(df.groupby('Gender')['Amount'].sum())
plt.figure(figsize=(8,8))
sns.barplot(x='Gender',y='Amount',data=df,estimator=sum,palette=['pink','blue'],edgecolor='black');
# Females spend more amount on credit card than males
# In[50]:
print(df.groupby(["Gender","Exp Type"])["Amount"].sum())
plt.figure(figsize=(7,7))
sns.barplot(x="Exp Type",y="Amount",hue="Gender",data=df,estimator=sum,palette=['pink','blue'],edgecolor='black')
# In[51]:
print(df.groupby(['Gender','Month'])['Amount'].sum().sort_values( ascending=False))
plt.figure(figsize=(14,14))
sns.barplot(x='Gender',y="Amount",hue='Month',data=df,estimator=sum,edgecolor='black')
# Females spend more in october while Males spend more in january.
#
# In[52]:
print(df.groupby(['Gender','Day'])['Amount'].sum().sort_values(ascending=False))
plt.figure(figsize=(8,8))
sns.barplot(x='Gender',y='Amount',hue='Day',data=df,estimator=sum,edgecolor='black')
plt.legend(loc='center right', bbox_to_anchor=(1.3,0.9))
# Both females and males spend more on sunday.
# In[53]:
print(df.groupby(["Gender","Exp Type"])["Amount"].sum().sort_values(ascending=False))
plt.figure(figsize=(14,14))
sns.barplot(x="Exp Type",y="Amount",hue="Gender",data=df,estimator=sum,palette=['pink','blue'],edgecolor="black")
# In[54]:
# What was the montly expense type of males
male=df[df['Gender']=='M']
print(male.groupby(['Month','Exp Type'])['Amount'])
plt.figure(figsize=(9,9))
sns.lineplot(x='Month',y='Amount',data=male,hue='Exp Type',estimator=sum,ci=None)
# In[55]:
# What was the montly expense type of both males and females
female=df[df['Gender']=='F']
print(female.groupby(["Month","Exp Type"])["Amount"].sum().sort_values(ascending=False))
plt.figure(figsize=(14,14))
sns.lineplot(x='Month',y='Amount',data=female, hue='Exp Type',ci=None,estimator=sum)
plt.show()
# In[56]:
print(df.groupby(["Month","Exp Type"])["Amount"].sum().sort_values(ascending=False))
plt.figure(figsize=(12,12))
sns.lineplot(x='Month',y='Amount',data=df,hue="Exp Type",ci=None,estimator=sum,dashes=True,style="Exp Type",sort=True)
plt.show()
# In[57]:
df.sample()
# In[58]:
weekday_df=df[df["Weekday"]<6]
weekend_df=df[df["Weekday"]>=6]
print("Weekday spends:",weekday_df["Amount"].sum())
print("Weekend spends:",weekend_df["Amount"].sum())
# In[59]:
print(weekday_df["Exp Type"].value_counts())
plt.figure(figsize=(9,9))
sns.countplot(x="Exp Type",data=weekday_df,edgecolor='black')
plt.show()
# In[60]:
print(weekend_df["Exp Type"].value_counts())
plt.figure(figsize=(11,11))
sns.countplot(x="Exp Type",data=weekend_df,edgecolor='black')
plt.show()
# In[61]:
print(weekday_df.groupby(['Gender','Exp Type'])['Amount'].sum())
plt.figure(figsize=(9,9))
sns.barplot(x='Exp Type',y='Amount',hue='Gender',data=weekday_df,edgecolor='black',estimator=sum)
plt.show()
# In[62]:
print(weekend_df.groupby(['Gender','Exp Type'])['Amount'].sum())
plt.figure(figsize=(10,10))
sns.barplot(x='Exp Type',y='Amount',hue='Gender',data=weekend_df,edgecolor='black',estimator=sum)
plt.show()
# In[63]:
x=df.groupby('City')['Amount'].sum().sort_values(ascending=False).reset_index().head(10)
print(x)
plt.figure(figsize=(9,9))
sns.barplot(x='City',y='Amount',data=x,estimator=sum,edgecolor='black')
plt.xticks(rotation=90)
plt.show()
# In[64]:
#Relationship between male and female with amount with respect to day
sns.relplot(x='Day',y='Amount',hue='Gender',data=df,kind='line',ci=None)
plt.xticks(rotation=90)
plt.show()
# In[65]:
print(df.groupby(['Gender','Day'])['Amount'].sum())
sns.barplot(x='Day',y='Amount',hue='Gender',data=df)
plt.xticks(rotation=90)
plt.legend(loc='center right',bbox_to_anchor=(1.3,0.7))
plt.show()
# In[66]:
df.sample()
# In[67]:
#which card type was mostly used by males
male=df[df['Gender']=='M']
l=["Platinum","Silver","Signature","Gold"]
x=male.groupby(['Card Type'])['Amount'].sum().sort_values(ascending=False)
print(x)
plt.figure(figsize=(6,6))
plt.pie(x,labels=l,shadow=True,autopct='%0.01f%%',wedgeprops={'width':0.1},textprops={'fontsize':20})
plt.show()
# In[68]:
#Which card type was mostyly used by Females
female=df[df['Gender']=="F"]
l=['Silver','Signature','Platinum','Gold']
f=female.groupby(['Card Type'])['Amount'].sum().sort_values(ascending=False)
print(f)
plt.figure(figsize=(7,7))
plt.pie(f,labels=l,shadow=True,autopct='%0.01f%%',textprops={'fontsize':20},wedgeprops={'width':0.1})
plt.show()
# In[69]:
#Monthly distributation of amount by Gender
sns.relplot(x='Month',y='Amount',hue="Gender",data=df,kind="line")
plt.xticks(rotation=90)
plt.show()
# In[70]:
#Top 5 cities males and females spend most amount on credit card
male=df[df['Gender']=='M']
x=male.groupby(['City'])['Amount'].sum().sort_values(ascending=False).head(5)
print(x)
l=['Ahmedabad','Bengaluru','Greater Mumbai','Delhi','Kolkata']
plt.pie(x,labels=l,shadow=True,autopct='%0.01f%%',textprops={'fontsize':20},wedgeprops={'width':0.8},explode=(0.1,0.1,0.1,0.1,0.1))
plt.legend(loc='center right',bbox_to_anchor=(1.9,0.8))
plt.show()
# Males are spending more in Ahmedabad using credit card as compared to any other city.
# In[71]:
female=df[df['Gender']=='F']
x=female.groupby(['City'])['Amount'].sum().sort_values(ascending=False).head(5)
print(x)
l=['Greater Mumbai','Bengaluru','Delhi','Ahmedabad','Kanpur']
plt.pie(x,labels=l,shadow=True,autopct='%0.01f%%',textprops={'fontsize':18},wedgeprops={'width':0.8},explode=(0.1,0.1,0.1,0.1,0.1))
plt.legend(loc=('center right'),bbox_to_anchor=(2,0.8))
plt.show()
# Females are spending more in Greater Mumbai using credit card as compared to any other city.
# In[72]:
#Which Exp type creates huge amount
df.groupby(['Exp Type'])['Amount'].sum().sort_values(ascending=False)
sns.barplot(x='Exp Type',y='Amount',data=df,edgecolor='black',estimator=sum)
# In[73]:
#Which is the min amount with details
df[df["Amount"]==df['Amount'].min()]
# In[74]:
#Which is the max amount with details
df[df['Amount']==df['Amount'].max()]
# In[75]:
get_ipython().system('pip install wordcloud')
# In[76]:
from wordcloud import WordCloud as word
d=df["City"].value_counts()
wc= word(background_color='white',height=1000,width=600)
wc.generate_from_frequencies(d)
# In[77]:
plt.figure(figsize=(15,10),dpi=100)
plt.imshow(wc)
plt.axis('off')
plt.show()
# Credit spends were more in the highlited cities
# In[78]:
df.to_csv("Credit Card Spending Habits (EDA).csv")
# ## Insights
# * Mostly Silver card types were used for payments.
# * In Expense types Fuel and Food expenses have recorded most counts paid by the cards.
# * as per month wise January and December have recorded high usage of cards payments and by day wise sundays have recorded more no. of card payments.
# * Bengaluru Greater Mumbai, Ahmedabad, Delhi These are the Top 4 Cities where card usage were more.
# * Females have used the Silver card type more where as males have used the platinum card types more.
# * Females are more dependent on credit cards as they spend more amount through credit cards than men.
# * Both Females and Males mostly spend amount on paying Bills and Food using credit card.
# * Females mostly spend in October and Males in january using credit cards.
# * Males have paid highest in january in Fuel expense where as females have paid highest in october in Bills.
# * Weekday Amount spends were more than weekends and that were mostly on Food and entertainment by each gender.
# * Males spend the highest amount with Platinum card type and Females spend the highest amount with Silver card type.
# * Females are spending more in Greater Mumbai where as males are spending more in Ahmedabad.
# * In bills payment the amount spent is the highest.
# In[ ]:
# In[ ]: