-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path19pandasIntro.py
1221 lines (651 loc) · 19.3 KB
/
19pandasIntro.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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#Panda Introduction and Topics
#-----------------------------
#%library
import pandas as pd
dir("D:\ML-Lab\CBAP_13DEC")
print(dir(pd), end =' ,')
len(dir(pd))
#initial
#help
#pd.read_csv?
#pip install pydataset
#read from dataset
from pydataset import data
data('')
mtcars = data('mtcars')
mtcars
mtcars.head(5)
#write to csv
mtcars.to_csv('D:\\airline.txt')
#read a csv/ excel
import pandas as pd
#df
df = pd.read_csv('D:\\airline.txt')
df.shape
df
#pandas Series
s = pd.Series(range(1, df.shape[0]+1))
s
ps2=df.set_index(s)
ps2
#should be able to read from CSV/ only data table
#create Panda Series
#create Pandas DF
string1 = "student"
string1
for i in range(65, 100):
print(string1 + str(chr(i)))
#You can get this error if you have variable str and trying to call str() function.
list1 = [1,2,3,4,5]
list2=['Student']
import pandas as pd
print(pd.__version__)
#panda series - single column values
ps1= pd.Series([1,4,8,11,65], dtype='int32')
#creates row index also
ps1
ps1.values
ps1.index
ps1[0]
ps1[1:4]
#assign new index when creating data
ps2=pd.Series([1,34,64,23,35], index=['a','b','c','d','e'])
ps2
#duplicate indexing
ps3=pd.Series([1,34,64,23,35], index=['a','b','c','d','d'])
ps3['d']
#pd.Series([1,34,64,23,35], index=[1,7,9,11])#error
pd.Series([1,34,64,23,35], index=[1,7,9,11,12])
ps2['c']
ps3['d'] #two values
ps2['a':'e']
#another way
ps4 = pd.Series({'a':1,'b':34,'c':64,'d':23,'e':35})
ps4
#indexing in Series
ps4[0]
ps4['a']
ps4[1:4]
ps4['a':'c']
ps4.loc['a'] #explicit index
ps4.iloc[1:3] #implicit index 0,1,..
ps4[['a','c']] #fancy indexing (selected)
ps4
ps4[ps4 > 30]
ps4[(ps4 > 30) & (ps4 < 50)]
ps4.keys
'a' in ps4 #index values check
ps4['a'] = 99 #change / mutate
ps4
#%%
import pandas as pd
#pandas data frame - multi column
#first create two series
course = pd.Series(['BTech','MTech','BBA','MBA'])
strength = pd.Series([100, 50, 200, 75])
fees = pd.Series([2.5, 3, 2, 4])
course, strength, fees
pd1 = pd.DataFrame([course, strength,fees])
pd1 #not the correct method of DF
pd2 = pd.DataFrame({'course':course, 'strength':strength, 'fees':fees})
d1={'course':course, 'strength':strength, 'fees':fees}
d1['course']
#count ?
pd2 #better way
pd2.index
pd2.columns
pd2.values
pd2['course']
pd2.keys
pd2[1:2] #row
pd2[1:2] #row
#indexeers - loc, iloc, ix
pd2
pd2.count()
pd2.course
pd2.columns
pd2.strength
pd2.fees
pd2 is pd2
pd2.course is pd2.fees
pd2.course is pd2['course'] #same method of access check
pd2.loc[1:3]
pd2.iloc[1:2] #no explicit here
pd2
pd2.course=='MBA'
pd2.loc[pd2.course=='BBA']
pd2.loc[pd2.course=='MBA', 'course':'fees']
pd2.loc[pd2.course=='MBA', ['course','fees']]
pd2
pd2.iloc[1,2]
pd2.iloc[1,2] = 5.5
pd2
pd3=pd2
pd3.course is pd2.course
pd2[pd2.fees >= 20000]
#index alignment - Pd 117 - do later
#%%
#Operating on data
import numpy as np
import pandas as pd
arr = np.random.randint(0,10,4)
ser = pd.Series(arr)
ser
#df
arr1 = np.random.randint(0,10,(3,4))
arr1
df = pd.DataFrame(arr1)
df
import numpy as np
rng = np.random.randint(0,10,(3,4))
#arr2
#df = pd.DataFrame(arr2, columns=['A','B','C','D'], index=['a','b','c'])
#df
#missing data
import pandas as pd
import numpy as np
placed = pd.Series([None,np.nan, 100, None])
placed
np.sum(placed) #working
course = pd.Series(['BTech','MTech','BBA','MBA'])
strength = pd.Series([100, 50, 200, 75])
fees = pd.Series([2.5, 3, 2, 4])
pd3 = pd.DataFrame({'course':course, 'strength':strength, 'fees':fees, 'placed':placed})
pd3
pd3.index
pd3['course']=='MTech'
pd3[pd3['course']=='MTech']
pd3[pd3['course']=='MTech'].index
pd3.drop(1)
pd3.drop(pd3[pd3['course']=='BTech'].index)
pd3.placed.sum()
pd3.strength.sum()
pd3.placed.max()
pd3.strength.max()
placed
placed.isnull()
placed.notnull()
p2=placed.dropna()
p2
placed
import pandas as pd
#new dataframe from list values
pd4 = pd.DataFrame([['dhiraj', 50, 'M', 10000, None], [None, None, None, None, None], ['kanika', 28, None, 5000, None], ['tanvi', 20, 'F', None, None], ['poonam',45,'F',None,None],['upen',None,'M',None, None]])
pd4
pd4.dropna() #row with any an NA value / completecases
pd4.dropna(axis='columns') #only those columns with NA
pd4.dropna(axis=1)
pd4.dropna(axis='rows') #rows
pd4.dropna(axis='rows', how = 'all')
pd4.dropna(axis='columns', how='all') #all None columns
pd4.dropna(axis='columns', how='any') #all None columns
pd4.dropna(axis='columns', thresh=3) #min non NA values in columns
pd4.dropna(axis='rows', thresh=3) #min min NA values in rows
#filling na values
placed= pd.Series([1,2, None, 5, None, None, 8])
placed
placed.fillna(0)
placed
placed.fillna(method='ffill')
placed.fillna(method='bfill')
#DF fill
pd4
pd4.fillna(method='ffill', axis=1)
pd4.fillna(method='ffill', axis=0)
#MultiIndex - Later
#pag 130
x= [1,2,3]
y= [4,5,6]
z= [7,8,9]
x,y,z
np.concatenate([x,y,z])
np.concatenate([x,y,z], axis=0) #they are single dim arrays
#np.concatenate([x,y,z], axis=1) #will not work
x=[[1,2,3],[4,5,6]]
y=[[10,11,12],[13,14,15]]
x
y
np.concatenate([x,y], axis=1) #cbind
np.concatenate([x,y], axis=0) #rbind
#DF
grades1 = pd.DataFrame(['A','B1'])
grades1
grades1 = {'subject1': ['A1','B1','A2','A3'],'subject2': ['A2','A1','B2','B3'] }
grades1
df1 = pd.DataFrame(grades1)
df1
grades2 = {'subject3': ['A1','B1','A2','A3'],'subject4': ['A2','A1','B2','B3']}
df2 = pd.DataFrame(grades2)
df2
#join
pd.concat([df1,df2])
pd.concat([df1,df2], axis=0)
pd.concat([df1,df2], axis=1)
pd.concat([df1,df2], ignore_index=True) #index values in order
pd =pd.concat([df1,df2], keys=['x','y']) #adding multiple index
pd.index
print (df)
'''
Create a MultiIndex:
mi = pd.MultiIndex.from_arrays((list('abc'), list('def')))
mi.names = ['level_1', 'level_2']
Get level values by supplying level as either integer or name:
mi.get_level_values(0)
Index(['a', 'b', 'c'], dtype='object', name='level_1')
mi.get_level_values('level_2')
Index(['d', 'e', 'f'], dtype='object', name='level_2')
'''
(
import pandas as pd
#Join
rollno = pd.Series(range(1,11))
rollno
[ "Student" + str(i) for i in range(1,11)]
list(range(1,11))
name = pd.Series(["student" + str(i) for i in range(1,11)])
name
genderlist = ['M','F']
import random
gender = random.choices(genderlist, k=10)
gender
random.choices(population=genderlist,weights=[0.4, 0.6],k=10)
import numpy as np
#numpy.random.choice(items, trials, p=probs)
np.random.choice(a=genderlist, size=10, p=[.2,.8])
import numpy as np
marks1 = np.random.randint(40,100,size=10)
marks1
pd5 = pd.DataFrame({'rollno':rollno, 'name':name, 'gender':gender, 'marks1':marks1})
pd5
#course = random.choices( population=['BBA','MBA','BTECH'] ,weights=[0.4, 0.3,0.3],k=10)
course = np.random.choice(a=['BBA','MBA','BTECH'], size=10)
course
marks2 = np.random.randint(40,100,size=10)
marks2
pd6 = pd.DataFrame({'rollno':rollno, 'course':course, 'marks2':marks2})
pd6
pd5
fees = pd.DataFrame({'course':['BBA','MBA','BTECH', 'MTECH'], 'fees':[100000, 200000, 150000, 220000]})
fees
#
pd5
pd6
#1 to 1
pd7=pd.merge(pd5, pd6)
pd7
pd7.head(2)
import pandas as pd
S1=pd.Series(range(1,11), dtype='int')
S1
name = pd.Series(["student" + str(i) for i in range(1,11)])
name
dftest1= pd.DataFrame({'rollno':S1, 'Name':name})
dftest1
S2=pd.Series(range(4,14))
# -*- coding: utf-8 -*-
pd.set_option('display.max_columns',15)
#others - max_rows, width, precision, height, date_dayfirst, date_yearfirst
import pandas as pd
import numpy as np
#read data
df = pd.read_csv('20denco.csv')
#see properties of data
df.head()
df.columns
len(df)
df.dtypes
df['partnum']=(df['partnum']).astype('str')
df.dtypes
df1 = df.describe()
df1
df.shape
df.dtypes
df.head()
'''
df['region'].unique()
df['region'] = df['region'].astype('category')
df['region'].unique()
'''
df.head()
df.region.value_counts()
df.region.value_counts().plot(kind='bar')
#do summary actions
#Who are the most loyal Customers - Improve repeated sales, Target customers with low sales Volumes
#count by customers, sort, pick top 5
df.columns
#numpy method of counts
df.custname.value_counts()
df.custname.value_counts().sort_values(ascending=False).head(5)
#pandas way
df.groupby('custname').size()
df.groupby('custname').size().sort_values(ascending=False)
#pickup first 5
df.groupby('custname').size().sort_values(ascending=False).head(5)
#these are most loyal customers
#extra- ??
'''
df.groupby(['custname'])['margin'].nlargest(3)
df.sort_values(['revenue'], ascending= True).groupby( 'region' ).mean()
'''
#Which customers contribute the most to their revenue - How do I
#retain these customers & target incentives
#find total revenue of each customer, sort in descending.
df.groupby('custname').aggregate({'revenue':np.sum})
df.groupby('custname').aggregate({'revenue':np.sum}).sort_values(by='revenue', ascending=False)
df.groupby('custname').aggregate({'revenue':np.sum}).sort_values(by='revenue', ascending=False).head(5)
#these are top 5 customers who gave revnue to Denco
#extras ???
'''
df.groupby('custname').aggregate({'revenue':[np.sum, 'size'] }).sort_values(by='revenue', ascending=False) #error
df[['revenue','custname']].groupby('custname').agg['size']
.sort_values(by='revenue', ascending=False) #error
'''
#What part numbers bring in to significant portion of revenue -
#Maximise revenue from high value parts
#grouby partnum, find value of revenue, sell these more
df[['partnum','revenue']].sort_values(by='revenue', ascending=False)
df[['partnum','revenue']].sort_values(by='revenue', ascending=False).head(5)
#these are top parts which give max revenue
#if total sales has to be considered
df[['partnum','revenue']].groupby('partnum').sum()
df[['partnum','revenue']].groupby('partnum').sum().sort_values(by='revenue', ascending=False).head(5)
#this total revenue value giving items
gdf = df[['partnum','revenue']].groupby(['partnum'], as_index=False).agg([sum, np.mean])
gdf.to_csv("gdf.csv", index=False)
gdf = pd.read_csv("gdf.csv")
gdf
gdf=gdf[2:]
gdf = gdf.rename(columns = {'Unnamed: 0':'Partnum', 'revenue': 'revenue_Sum', 'revenue.1': 'revenue_mean'}).reindex()
gdf
gdf.sort_values(by='revenue_mean', ascending=False).head(5)
df.dtypes
#What parts have the highest profit margin - What parts are driving profits &
#what parts need to build further
#check for margin value, their individual margin and total sales margin like revenue
df[['partnum','margin']].sort_values(by='margin', ascending=False)
df[['partnum','margin']].sort_values(by='margin', ascending=False).head(5)
df[['partnum','margin']].sort_values(by='margin', ascending=False).tail(5)
#these are top parts which give max margins
#if total sales has to be considered
pd.set_option('display.max_columns',1000)
df[['partnum','margin']].groupby('partnum').sum()
df1= df[['partnum','margin']].groupby('partnum').sum().sort_values(by='margin', ascending=False).head(5)
df1
df1.to_csv('aa.csv')
#this total revenue value giving items
#Most sold items
df.groupby('partnum').size().sort_values(ascending=False).head(5)
#which regions gave max revenue
df2 = df[['revenue', 'region']].groupby( 'region').sum().sort_values(by='revenue', ascending=False).head(5)
df2
#East gave max revenue
df[['revenue', 'region']].groupby( 'region').sum().sort_values(by='revenue', ascending=False).plot(kind='barh')
S2
name = pd.Series(["student" + str(i) for i in range(20,30)])
name
dftest2= pd.DataFrame({'rollno':S2, 'Name':name})
dftest2
dftest1
pd.merge(dftest1, dftest2, on='rollno')
pd5
pd6
pd8=pd.merge(pd5, pd6, on='rollno')
pd8
pd8.head(2)
#many to 1
pd6
fees
pd.merge(pd6, fees, on = 'course')
#
pd6b = pd.DataFrame({'rollno1':rollno, 'course':course, 'marks3':marks2})
pd6b
pd5
pdmerge= pd.merge(pd5, pd6b, left_on='rollno', right_on='rollno1')
pdmerge
pdmerge.drop('rollno1', axis=1)
rollno1 = pd.Series(range(4, 14))
pd6b = pd.DataFrame({'rollno1':rollno1, 'course':course, 'marks3':marks2})
pd6b
pd5
pdmerge= pd.merge(pd5, pd6b, left_on='rollno', right_on='rollno1')
pdmerge
pdmerge.drop('rollno1', axis=1)
pd.merge(pd5, pd6b, left_on='rollno', right_on='rollno1').drop('rollno1', axis=1).head(2) #drop redundant coln
pd5
#with indices defined
pd5a = pd5.set_index('rollno')
pd5a
pd6
pd6a = pd6.set_index('rollno')
pd6a
pd5a
pd.merge(pd5a, pd6a, left_index=True, right_index=True)
pd.merge(pd5a, pd6, left_index=True, right_on='rollno')
pd5a
pd6a
#pd6a.join(pd5a)
pd5a
pd6
pd.merge(pd5a, pd6, left_index=True, right_on='rollno')
#set arithmetic
pd5
pd6
pd.merge(pd5, pd6, how='inner')
fees
pd6
pd.merge(pd6, fees, how='inner')
#two joins
#left - combining on the basis of left df
#right -combining on the basis of right df
pd6
fees
pd.merge(pd6, fees, how='outer')
pd6
fees
pd.merge(pd6, fees, how='left')
pd.merge(fees, pd6, how='right')
pd5
pd6
pd4
pd4.columns
pd4.columns = ['name', 'age','gender','fees']
pd4
pd4.apply(pd.value_counts)
pd4.age
#select all from df where age>20
pd4.age > 20
pd4[pd4.age > 20]
#find out if any value in df.age is null then results True
pd4.age.isnull().any()
pd4
pd4.isnull()
pd4.isnull().any()
pd4.head(3)
pd4
pd4.dropna()
pd4
pd4.fillna(method='ffill')
pd4.fillna(method='bfill')
#pd4.dropna(inplace=True) #???? carefull it will make changes to original
pd4
pd4.columns
pd4.columns = ['name', 'age','gender','fees', 'NIL']
pd4
pd4.sort_values(ascending=True, by='name')
pd4.sort_values(ascending=False, by='name')
pd4 = pd.DataFrame([['dhiraj', 50, 'M', 10000], ['kanika', 28, None, 5000], ['tanvi', 20, 'F', None], ['upen',None,'M',None]])
pd4
pd4.columns = ['name', 'age','gender','fees']
pd4_c =pd4.copy()
pd4
pd4a= pd4.sort_values(ascending=True, by='name', inplace = True)
pd4a
pd4b=pd4.sort_values(ascending=False, by='name', inplace=False)
pd4b
pd4b=pd4.sort_values(ascending=False, by='name',inplace=True)
pd4b #change original DF
pd4
pd55= pd4.dropna(axis='index', how='all', inplace=False)
pd55
pd4
pd4.sort_values(ascending=False, by=['age'])
pd4.sort_values(ascending=False, by=['fees'], na_position='first')
pd4.sort_values(ascending=True, by=['gender','age'])
pd4.sort_values(ascending=[True,False], by=['gender','age'])
#pd4.sort() #depreciated
pd4.shape
#%%
#summary on DF: create fairly large data
import pandas as pd
import numpy as np
rollno = pd.Series(range(1,1001))
rollno
name = pd.Series(["student" + str(i) for i in range(1,1001)])
name
genderlist = ['M','F']
import random
#gender = random.choices(genderlist, k=1000)
gender= np.random.choice(a=genderlist, size=1000,replace=True, p=[.6,.4])
gender
import collections
collections.Counter(gender)
marks1 = np.random.randint(40,100,size=1000)
marks2 = np.random.randint(40,100,size=1000)
fees = np.random.randint(50000,100000,size=1000)
fees.mean()
course = np.random.choice(a=['BBA','MBA','BTECH', 'MTech'], size=1000, p=[0.4, 0.5,0.09,0.01])
course
collections.Counter(course)
city = np.random.choice(a=['Delhi', 'Gurugram','Noida','Faridabad'], size=1000, replace=True, p=[.4,.2,.2,.2])
collections.Counter(city)
course = np.random.choice(a=['BBA','MBA','BTECH', 'MTech'], size=1000, p=[0.4, 0.5,0.09,0.01])
pd8 = pd.DataFrame({'rollno':rollno, 'name':name, 'course':course, 'gender':gender, 'marks1':marks1,'marks2':marks2, 'fees':fees,'city':city})
pd8
pd8.head(1)
pd8.shape
#start
pd8.head()
pd8.tail()
pd8.describe()
pd8.count()
pd8['gender'].value_counts() #if col has spaces
pd8.gender.value_counts()
pd8.apply(pd.value_counts)
#pd8.apply(pd.value_counts).fillna(0)
pd8
pdg=pd8.groupby('course')
pdg
pd8.groupby('course').size()
pd8.groupby('course').count()