-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
1051 lines (871 loc) · 29.6 KB
/
index.html
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
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/os-theme.css">
<link rel="stylesheet" href="css/zenburn.css">
</head>
<body>
<div class="reveal">
<div class="slides">
<section class="intro">
<img class="logo" src="images/logo-home.png">
<h1>How to hug <strong>Pandas</strong></h1>
<div class="fecha">Eyad Tomeh. PyconEs, Septiembre 2017</div>
</section>
<section class="green">
<h1>What is<strong> Pandas?</strong></h1>
<p>pandas is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language.</p>
<img src="images/laptop-hands.png" alt="" class="detail">
</section>
<section class="white">
<h1>Why<strong> pandas?</strong></h1>
<h2>What makes pandas different</h2>
<ul>
<li>
A fast and efficient DataFrame object for data manipulation
</li>
<li>
Tools for reading/writing data between in-memory data structures and different formats...
</li>
<li>
Intelligent data alignment and integrated handling of missing data..
</li>
<li>
High performance merging and joining of data sets
</li>
<li>
Time series-functionality: date range generation and frequency conversion...
</li>
<li>
Highly optimized for performance, with critical code paths written in Cython or C
</li>
<li>
Python with pandas is in use in a wide variety of academic and commercial domains...
</li>
</ul>
</section>
<section class="white">
<h1>Pandas <strong>Data types</strong></h1>
<h2>Series</h2>
<p>A pandas Series combines the idea of a list with an additional index column, by default this is a numeric index starting at zero</p>
<pre><code class="python" data-trim>
series = pd.Series( ['one', 'two', 'three', 'four'] )
print(series)
print(series.index)
</code></pre>
<pre><code class="python">
0 one
1 two
2 three
3 four
dtype: object
RangeIndex(start=0, stop=4, step=1)
</code></pre>
<img src="images/laptop-hands.png" alt="" class="detail">
</section>
<section class="white">
<h1>Pandas <strong>Data types</strong></h1>
<h2>Dataframes</h2>
<p>DataFrames are two-dimensional data tables in which rows of data have values spread across one or more columns, much like a sheet in a spreadsheet. Each column behaves as if it is a Series; a DataFrame can thus be thought of as a dict of Series, where dict keys correspond to column names</p>
<pre><code class="python" data-trim>
import pandas as pd
# loading a dict
shinyData = {
'project': ['mio', 'todoslosdemas'],
'points':[12912, 1],
'tech': ['pandas', 'mascosas']
}
shiny_df = pd.DataFrame(shinyData)
print(shiny_df)
</code></pre>
<pre><code class="python">
points project tech
0 12912 mio pandas
1 1 todoslosdemas mascosas
</code></pre>
<img src="images/slider01.png" alt="" class="detail">
</section>
<section class="green">
<h1>Loading <strong>data</strong></h1>
<h2>Importing data into dataframes</h2>
<pre><code class="python" data-trim>
import pandas as pd
# loading a csv file
shiny_df = pd.from_csv('path/to/file.csv', sep=',', skiprows=5)
shiny_df = pd.from_json('path/to/file.json')
shiny_df = pd.read_sql('SELECT points, project, tech FROM open_shine_data', con=connection)
shiny_df = pd.read_sql_table('open_shine_data', con=connection)
print(shiny_df)
</code></pre>
<pre><code class="python">
points project tech
0 12912 mio pandas
1 1 todoslosdemas mascosas
</code></pre>
<img src="images/slider03.png" alt="" class="detail">
</section>
<section class="white">
<h1>Getting <strong>statistics</strong></h1>
<p>Dataframe</p>
<pre><code class="python" data-trim>
d = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
print(df)
</code></pre>
<pre><code class="python">
one two
a 1.0 1.0
b 2.0 2.0
c 3.0 3.0
d NaN 4.0
</code></pre>
<p>Describe</p>
<pre><code class="python" data-trim>
print(df.describe())
</code></pre>
<pre><code class="python">
one two
count 3.0 4.000000
mean 2.0 2.500000
std 1.0 1.290994
min 1.0 1.000000
25% 1.5 1.750000
50% 2.0 2.500000
75% 2.5 3.250000
max 3.0 4.000000
</code></pre>
</section>
<section class="white">
<h1>Getting <strong>statistics</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
one two
a 1.0 1.0
b 2.0 2.0
c 3.0 3.0
d NaN 4.0
</code></pre>
<p>Sum</p>
<pre><code class="python" data-trim>
print(df.sum())
print(df.one.sum())
</code></pre>
<pre><code class="python" data-trim>
one 6.0
two 10.0
dtype: float64
6.0
</code></pre>
<p>Mean</p>
<pre><code class="python" data-trim>
print(df.mean())
print(df.one.mean())
</code></pre>
<pre><code class="python" data-trim>
one 2.0
two 2.5
dtype: float64
2.0
</code></pre>
</section>
<section class="white">
<h1>Getting <strong>statistics</strong></h1>
<p>Dataframe</p>
</code></pre>
<pre><code class="python">
one two
a 1.0 1.0
b 2.0 2.0
c 3.0 3.0
d NaN 4.0
</code></pre>
<p>Cumulative sum</p>
<pre><code class="python" data-trim>
print(df.cumsum())
print(df.two.cumsum())
</code></pre>
<pre><code class="python">
one two
a 1.0 1.0
b 3.0 3.0
c 6.0 6.0
d NaN 10.0
a 1.0
b 3.0
c 6.0
d 10.0
Name: two, dtype: float64
</code></pre>
</section>
<section class="white">
<h1>Indexing <strong>DataFrames</strong></h1>
<p>Dataframe</p>
<pre><code class="python" data-trim>
d = {'name' : ['Algo', 'Otro', 'Nuevo', 'Antiguo', 'Entre los dos'],
'value' : [21, 32, 65, 69, 31],
'date': ['2017-01-01', '2017-02-01', '2017-03-01', '2017-04-01', '2017-04-01']}
df = pd.DataFrame(d)
print(df)
</code></pre>
<pre><code class="python">
date name value
0 2017-01-01 Algo 21
1 2017-02-01 Otro 32
2 2017-03-01 Nuevo 65
3 2017-04-01 Antiguo 69
4 2017-04-01 Entre los dos 31
</code></pre>
<p>Set index</p>
<pre><code class="python" data-trim>
print(df.set_index('name'))
</code></pre>
<pre><code class="python">
date value
name
Algo 2017-01-01 21
Otro 2017-02-01 32
Nuevo 2017-03-01 65
Antiguo 2017-04-01 69
Entre los dos 2017-04-01 31
</code></pre>
</section>
<section class="white">
<h1>Indexing <strong>DataFrames</strong></h1>
<p>Reset index</p>
<pre><code class="python" data-trim>
df = df.set_index('name')
print(df)
print(df.reset_index())
</code></pre>
<pre><code class="python">
date value
name
Algo 2017-01-01 21
Otro 2017-02-01 32
Nuevo 2017-03-01 65
Antiguo 2017-04-01 69
Entre los dos 2017-04-01 31
name date value
0 Algo 2017-01-01 21
1 Otro 2017-02-01 32
2 Nuevo 2017-03-01 65
3 Antiguo 2017-04-01 69
4 Entre los dos 2017-04-01 31
</code></pre>
</section>
<section class="white">
<h1>Indexing <strong>DataFrames</strong></h1>
<p>Reset index</p>
<pre><code class="python">
# Filter
df = df[df['value'] < 60]
print(df)
# Reset
print(df.reset_index())
# Reset
print(df.reset_index(drop=True))
</code></pre>
<pre><code class="python">
date name value
0 2017-01-01 Algo 21
1 2017-02-01 Otro 32
4 2017-04-01 Entre los dos 31
</code></pre>
<pre><code class="python">
index date name value
0 0 2017-01-01 Algo 21
1 1 2017-02-01 Otro 32
2 4 2017-04-01 Entre los dos 31
</code></pre>
<pre><code class="python">
date name value
0 2017-01-01 Algo 21
1 2017-02-01 Otro 32
2 2017-04-01 Entre los dos 31
</code></pre>
</section>
<section class="white">
<h1>Indexing <strong>DataFrames</strong></h1>
<p>Dataframe</p>
<pre><code class="python" data-trim>
d = {'name' : ['Algo', 'Otro', 'Nuevo', 'Antiguo', 'Entre los dos'],
'value' : [21, 32, 65, 69, 31]}
df = pd.DataFrame(d, index=pd.DatetimeIndex(start='2017-01-01', end='2017-01-09', freq='2D'))
print(df)
print(df.index)
</code></pre>
<pre><code class="python">
name value
2017-01-01 Algo 21
2017-01-03 Otro 32
2017-01-05 Nuevo 65
2017-01-07 Antiguo 69
2017-01-09 Entre los dos 31
</code></pre>
<pre><code class="python">
DatetimeIndex(['2017-01-01', '2017-01-03', '2017-01-05', '2017-01-07',
'2017-01-09'],
dtype='datetime64[ns]', freq='2D')
</code></pre>
</section>
<section class="white">
<h1>Indexing <strong>DataFrames</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
name value
2017-01-01 Algo 21
2017-01-03 Otro 32
2017-01-05 Nuevo 65
2017-01-07 Antiguo 69
2017-01-09 Entre los dos 31
</code></pre>
<p>reindex</p>
<pre><code class="python" data-trim>
df = df.reindex(pd.DatetimeIndex(start='2017-01-01', end='2017-01-10', freq='D'))
print(df)
</code></pre>
<pre><code class="python">
name value
2017-01-01 Algo 21.0
2017-01-02 NaN NaN
2017-01-03 Otro 32.0
2017-01-04 NaN NaN
2017-01-05 Nuevo 65.0
2017-01-06 NaN NaN
2017-01-07 Antiguo 69.0
2017-01-08 NaN NaN
2017-01-09 Entre los dos 31.0
2017-01-10 NaN NaN
</code></pre>
</section>
<section class="white">
<h1>Indexing <strong>DataFrames</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
name value
2017-01-01 Algo 21.0
2017-01-02 NaN NaN
2017-01-03 Otro 32.0
2017-01-04 NaN NaN
2017-01-05 Nuevo 65.0
2017-01-06 NaN NaN
2017-01-07 Antiguo 69.0
2017-01-08 NaN NaN
2017-01-09 Entre los dos 31.0
2017-01-10 NaN NaN
</code></pre>
<p>ffill</p>
<pre><code class="python" data-trim>
print(df.ffill())
</code></pre>
<pre><code class="python">
name value
2017-01-01 Algo 21.0
2017-01-02 Algo 21.0
2017-01-03 Otro 32.0
2017-01-04 Otro 32.0
2017-01-05 Nuevo 65.0
2017-01-06 Nuevo 65.0
2017-01-07 Antiguo 69.0
2017-01-08 Antiguo 69.0
2017-01-09 Entre los dos 31.0
2017-01-10 Entre los dos 31.0
</code></pre>
</section>
<section class="white">
<h1>Indexing <strong>DataFrames</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
name value
2017-01-01 Algo 21.0
2017-01-02 NaN NaN
2017-01-03 Otro 32.0
2017-01-04 NaN NaN
2017-01-05 Nuevo 65.0
2017-01-06 NaN NaN
2017-01-07 Antiguo 69.0
2017-01-08 NaN NaN
2017-01-09 Entre los dos 31.0
2017-01-10 NaN NaN
</code></pre>
<p>bfill</p>
<pre><code class="python" data-trim>
print(df.bfill())
</code></pre>
<pre><code class="python">
name value
2017-01-01 Algo 21.0
2017-01-02 Otro 32.0
2017-01-03 Otro 32.0
2017-01-04 Nuevo 65.0
2017-01-05 Nuevo 65.0
2017-01-06 Antiguo 69.0
2017-01-07 Antiguo 69.0
2017-01-08 Entre los dos 31.0
2017-01-09 Entre los dos 31.0
2017-01-10 NaN NaN
</code></pre>
</section>
<section class="green">
<h1>Filtering <strong>DataFrames</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
date name value
0 2017-01-01 Algo 21
1 2017-02-01 Otro 32
2 2017-03-01 Nuevo 65
3 2017-04-01 Antiguo 69
4 2017-05-01 Entre los dos 31
</code></pre>
<pre><code class="python">
print(df[df['value'] < 60])
</code></pre>
<pre><code class="python">
date name value
0 2017-01-01 Algo 21
1 2017-02-01 Otro 32
4 2017-05-01 Entre los dos 31
</code></pre>
</section>
<section class="green">
<h1>Filtering <strong>DataFrames</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
date name value
0 2017-01-01 Algo 21
1 2017-02-01 Otro 32
2 2017-03-01 Nuevo 65
3 2017-04-01 Antiguo 69
4 2017-05-01 Entre los dos 31
</code></pre>
<pre><code class="python">
print(df[df['name'] == 'Algo'])
</code></pre>
<pre><code class="python">
date name value
0 2017-01-01 Algo 21
</code></pre>
</section>
<section class="green">
<h1>Filtering <strong>DataFrames</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
date name value
0 2017-01-01 Algo 21
1 2017-02-01 Otro 32
2 2017-03-01 Nuevo 65
3 2017-04-01 Antiguo 69
4 2017-05-01 Entre los dos 31
</code></pre>
<pre><code class="python">
print(df[df['name'].isin(['Algo', 'Nuevo'])])
</code></pre>
<pre><code class="python">
date name value
0 2017-01-01 Algo 21
2 2017-03-01 Nuevo 65
</code></pre>
</section>
<section class="green">
<h1>Filtering <strong>DataFrames</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
date name value
0 2017-01-01 Algo 21
1 2017-02-01 Otro 32
2 2017-03-01 Nuevo 65
3 2017-04-01 Antiguo 69
4 2017-05-01 Entre los dos 31
</code></pre>
<pre><code class="python">
print(df[(df['value'] < 60) & (df['date'] <= datetime(2017, 5, 1))])
</code></pre>
<pre><code class="python">
date name value
0 2017-01-01 Algo 21
1 2017-02-01 Otro 32
4 2017-05-01 Entre los dos 31
</code></pre>
</section>
<section class="green">
<h1>Filtering <strong>DataFrames</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
date name value
0 2017-01-01 Algo 21
1 2017-02-01 Otro 32
2 2017-03-01 Nuevo 65
3 2017-04-01 Antiguo 69
4 2017-05-01 Entre los dos 31
</code></pre>
<pre><code class="python">
print(df['name'].isin(['Algo', 'Nuevo']))
</code></pre>
<pre><code class="python">
0 True
1 False
2 True
3 False
4 False
Name: name, dtype: bool
</code></pre>
</section>
<section class="white">
<h1>Vector <strong>Operations</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
date name x y
0 2017-01-01 Algo 21 55
1 2017-02-01 Otro 32 47
2 2017-03-01 Nuevo 65 21
3 2017-04-01 Antiguo 69 78
4 2017-05-01 Entre los dos 31 89
</code></pre>
<pre><code class="python">
df['x_por_y'] = df['x'] * df['y']
df['x_sobre_y'] = df['x'] / df['y']
print(df)
</code></pre>
<pre><code class="python">
date name x y x_por_y x_sobre_y
0 2017-01-01 Algo 21 55 1155 0.381818
1 2017-02-01 Otro 32 47 1504 0.680851
2 2017-03-01 Nuevo 65 21 1365 3.095238
3 2017-04-01 Antiguo 69 78 5382 0.884615
4 2017-05-01 Entre los dos 31 89 2759 0.348315
</code></pre>
</section>
<section class="white">
<h1>Vector <strong>Operations</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
date name x y
0 2017-01-01 Algo 21 55
1 2017-02-01 Otro 32 47
2 2017-03-01 Nuevo 65 21
3 2017-04-01 Antiguo 69 78
4 2017-05-01 Entre los dos 31 89
</code></pre>
<pre><code class="python">
df['x_por_2'] = df['x'] * 2
df['formula'] = (((df['x'] / df['y']) * 100) - 3) *df['x']
print(df)
</code></pre>
<pre><code class="python">
date name x y x_por_2 formula
0 2017-01-01 Algo 21 55 42 738.818182
1 2017-02-01 Otro 32 47 64 2082.723404
2 2017-03-01 Nuevo 65 21 130 19924.047619
3 2017-04-01 Antiguo 69 78 138 5896.846154
4 2017-05-01 Entre los dos 31 89 62 986.775281
</code></pre>
</section>
<section class="white">
<h1>Apply <strong>Series</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
date name x y
0 2017-01-01 Algo 21 55
1 2017-02-01 Otro 32 47
2 2017-03-01 Nuevo 65 21
3 2017-04-01 Antiguo 69 78
4 2017-05-01 Entre los dos 31 89
</code></pre>
<pre><code class="python">
df['x_por_2'] = df['x'].apply(lambda x: x * 2)
print(df)
</code></pre>
<pre><code class="python">
date name x y x_por_2
0 2017-01-01 Algo 21 55 42
1 2017-02-01 Otro 32 47 64
2 2017-03-01 Nuevo 65 21 130
3 2017-04-01 Antiguo 69 78 138
4 2017-05-01 Entre los dos 31 89 62
</code></pre>
</section>
<section class="white">
<h1>Apply <strong>Series</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
date name x y
0 2017-01-01 Algo 21 55
1 2017-02-01 Otro 32 47
2 2017-03-01 Nuevo 65 21
3 2017-04-01 Antiguo 69 78
4 2017-05-01 Entre los dos 31 89
</code></pre>
<pre><code class="python">
def do_stuff_to_x_but_be_gentle(x):
if x in list_of_things:
return x * 2
else:
return x
df['formula'] = df['x'].apply(do_stuff_to_x_but_be_gentle)
</code></pre>
</section>
<section class="green">
<h1>Groupby <strong>Dataframe</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
type date name x y
0 Python-y 2017-01-01 Algo 21 55
1 Django-y 2017-01-01 Otro 32 47
2 Python-y 2017-01-02 Nuevo 65 21
3 Django-y 2017-01-02 Antiguo 69 78
4 Python-y 2017-01-03 Entre los dos 31 89
5 Django-y 2017-01-03 Meh 39 50
</code></pre>
<pre><code class="python">
print(df.groupby('type').sum())
print(df.groupby('type').mean())
</code></pre>
<pre><code class="python">
x y
type
Django-y 140 175
Python-y 117 165
</code></pre>
<pre><code class="python">
x y
type
Django-y 46.666667 58.333333
Python-y 39.000000 55.000000
</code></pre>
</section>
<section class="green">
<h1>Groupby <strong>Dataframe</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
type date name x y
0 Python-y 2017-01-01 Algo 21 55
1 Django-y 2017-01-01 Otro 32 47
2 Python-y 2017-01-02 Nuevo 65 21
3 Django-y 2017-01-02 Antiguo 69 78
4 Python-y 2017-01-03 Entre los dos 31 89
5 Django-y 2017-01-03 Meh 39 50
</code></pre>
<pre><code class="python">
print(df.groupby('type').first())
</code></pre>
<pre><code class="python">
date name x y
type
Django-y 2017-01-01 Otro 32 47
Python-y 2017-01-01 Algo 21 55
</code></pre>
</section>
<section class="green">
<h1>Groupby <strong>Dataframe</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
type date name x y
0 Python-y 2017-01-01 Algo 21 55
1 Django-y 2017-01-01 Otro 32 47
2 Python-y 2017-01-02 Nuevo 65 21
3 Django-y 2017-01-02 Antiguo 69 78
4 Python-y 2017-01-03 Entre los dos 31 89
5 Django-y 2017-01-03 Meh 39 50
</code></pre>
<pre><code class="python">
df = df.groupby('type').agg({
'date': 'count',
'name': 'last',
'x': 'sum',
'y': func_name # Mean in this example
})
print(df)
</code></pre>
<pre><code class="python">
date name x y
type
Django-y 3 Meh 140 58.333333
Python-y 3 Entre los dos 117 55.000000
</code></pre>
</section>
<section class="green">
<h1>Resample <strong>Dataframe</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
d = {'name' : ['Algo', 'Otro', 'Nuevo', 'Antiguo', 'Entre los dos', 'Meh'],
'x' : [21, 32, 65, 69, 31, 39],
'y' : [55, 47, 21, 78, 89, 50]}
df = pd.DataFrame(d, index=pd.DatetimeIndex(start='2017-01-01 00:00:00', end='2017-01-01 01:15:00', freq='15T'))
df = df[['name', 'x', 'y']]
print(df)
</code></pre>
<pre><code class="python">
name x y
2017-01-01 00:00:00 Algo 21 55
2017-01-01 00:15:00 Otro 32 47
2017-01-01 00:30:00 Nuevo 65 21
2017-01-01 00:45:00 Antiguo 69 78
2017-01-01 01:00:00 Entre los dos 31 89
2017-01-01 01:15:00 Meh 39 50
</code></pre>
</section>
<section class="green">
<h1>Resample <strong>Dataframe</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
name x y
2017-01-01 00:00:00 Algo 21 55
2017-01-01 00:15:00 Otro 32 47
2017-01-01 00:30:00 Nuevo 65 21
2017-01-01 00:45:00 Antiguo 69 78
2017-01-01 01:00:00 Entre los dos 31 89
2017-01-01 01:15:00 Meh 39 50
</code></pre>
<pre><code class="python">
print(df.resample('1h').sum())
</code></pre>
<pre><code class="python">
x y
2017-01-01 00:00:00 187 201
2017-01-01 01:00:00 70 139
</code></pre>
</section>
<section class="green">
<h1>Resample <strong>Dataframe</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
name x y
2017-01-01 00:00:00 Algo 21 55
2017-01-01 00:15:00 Otro 32 47
2017-01-01 00:30:00 Nuevo 65 21
2017-01-01 00:45:00 Antiguo 69 78
2017-01-01 01:00:00 Entre los dos 31 89
2017-01-01 01:15:00 Meh 39 50
</code></pre>
<pre><code class="python">
print(df.resample('30T').mean())
</code></pre>
<pre><code class="python">
x y
2017-01-01 00:00:00 26.5 51.0
2017-01-01 00:30:00 67.0 49.5
2017-01-01 01:00:00 35.0 69.5
</code></pre>
</section>
<section class="white">
<h1>More groupby <strong>Dataframe</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
d = {'name' : ['Algo', 'Otro', 'Nuevo', 'Antiguo', 'Entre los dos', 'Meh', 'Mas1', 'Mas2'],
'type': ['Python-y', 'Django-y', 'Python-y', 'Django-y', 'Python-y', 'Django-y', 'Python-y', 'Django-y'],
'date': ['2017-01-01 00:00:00', '2017-01-01 00:00:00',
'2017-01-01 00:15:00', '2017-01-01 00:15:00',
'2017-01-01 00:30:00', '2017-01-01 00:30:00',
'2017-01-01 00:45:00', '2017-01-01 00:45:00'],
'x' : [21, 32, 65, 69, 31, 39, 22, 34],
'y' : [55, 47, 21, 78, 89, 50, 92, 12]}
df = pd.DataFrame(d)
df = df[['type', 'date', 'name', 'x', 'y']]
df['date'] = pd.to_datetime(df['date'])
print(df)
</code></pre>
<pre><code class="python">
type date name x y
0 Python-y 2017-01-01 00:00:00 Algo 21 55
1 Django-y 2017-01-01 00:00:00 Otro 32 47
2 Python-y 2017-01-01 00:15:00 Nuevo 65 21
3 Django-y 2017-01-01 00:15:00 Antiguo 69 78
4 Python-y 2017-01-01 00:30:00 Entre los dos 31 89
5 Django-y 2017-01-01 00:30:00 Meh 39 50
6 Python-y 2017-01-01 00:45:00 Mas1 22 92
7 Django-y 2017-01-01 00:45:00 Mas2 34 12
</code></pre>
</section>
<section class="white">
<h1>More groupby <strong>Dataframe</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
type date name x y
0 Python-y 2017-01-01 00:00:00 Algo 21 55
1 Django-y 2017-01-01 00:00:00 Otro 32 47
2 Python-y 2017-01-01 00:15:00 Nuevo 65 21
3 Django-y 2017-01-01 00:15:00 Antiguo 69 78
4 Python-y 2017-01-01 00:30:00 Entre los dos 31 89
5 Django-y 2017-01-01 00:30:00 Meh 39 50
6 Python-y 2017-01-01 00:45:00 Mas1 22 92
7 Django-y 2017-01-01 00:45:00 Mas2 34 12
</code></pre>
<pre><code class="python">
print(df.groupby(['type', 'date']).sum())
</code></pre>
<pre><code class="python">
x y
type date
Django-y 2017-01-01 00:00:00 32 47
2017-01-01 00:15:00 69 78
2017-01-01 00:30:00 39 50
2017-01-01 00:45:00 34 12
Python-y 2017-01-01 00:00:00 21 55
2017-01-01 00:15:00 65 21
2017-01-01 00:30:00 31 89
2017-01-01 00:45:00 22 92
</code></pre>
</section>
<section class="white">
<h1>More groupby <strong>Dataframe</strong></h1>
<p>Dataframe</p>
<pre><code class="python">
type date name x y
0 Python-y 2017-01-01 00:00:00 Algo 21 55
1 Django-y 2017-01-01 00:00:00 Otro 32 47
2 Python-y 2017-01-01 00:15:00 Nuevo 65 21
3 Django-y 2017-01-01 00:15:00 Antiguo 69 78
4 Python-y 2017-01-01 00:30:00 Entre los dos 31 89
5 Django-y 2017-01-01 00:30:00 Meh 39 50
6 Python-y 2017-01-01 00:45:00 Mas1 22 92
7 Django-y 2017-01-01 00:45:00 Mas2 34 12
</code></pre>
<pre><code class="python">