-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.xml
2404 lines (2217 loc) · 184 KB
/
index.xml
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
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Lorenzo Bracciale</title>
<link>https://lorenzobracciale.github.io/</link>
<atom:link href="https://lorenzobracciale.github.io/index.xml" rel="self" type="application/rss+xml" />
<description>Lorenzo Bracciale</description>
<generator>Wowchemy (https://wowchemy.com)</generator><language>en-us</language><lastBuildDate>Wed, 07 Jun 2023 12:30:00 +0000</lastBuildDate>
<image>
<url>https://lorenzobracciale.github.io/media/icon_hu0b7a4cb9992c9ac0e91bd28ffd38dd00_9727_512x512_fill_lanczos_center_3.png</url>
<title>Lorenzo Bracciale</title>
<link>https://lorenzobracciale.github.io/</link>
</image>
<item>
<title>Lo stato di salute della sanità digitale in Italia</title>
<link>https://lorenzobracciale.github.io/talk/lo-stato-di-salute-della-sanita-digitale-in-italia/</link>
<pubDate>Wed, 07 Jun 2023 12:30:00 +0000</pubDate>
<guid>https://lorenzobracciale.github.io/talk/lo-stato-di-salute-della-sanita-digitale-in-italia/</guid>
<description></description>
</item>
<item>
<title>Dall'osservatorio alla definizione di un indice di rischio ospedaliero</title>
<link>https://lorenzobracciale.github.io/talk/dallosservatorio-alla-definizione-di-un-indice-di-rischio-ospedaliero/</link>
<pubDate>Wed, 17 May 2023 09:30:00 +0000</pubDate>
<guid>https://lorenzobracciale.github.io/talk/dallosservatorio-alla-definizione-di-un-indice-di-rischio-ospedaliero/</guid>
<description></description>
</item>
<item>
<title>Cryptocurrencies: great invention or scam of the century?</title>
<link>https://lorenzobracciale.github.io/talk/cryptocurrencies-great-invention-or-scam-of-the-century/</link>
<pubDate>Mon, 19 Sep 2022 18:00:00 +0000</pubDate>
<guid>https://lorenzobracciale.github.io/talk/cryptocurrencies-great-invention-or-scam-of-the-century/</guid>
<description></description>
</item>
<item>
<title>Pandas Tutorial</title>
<link>https://lorenzobracciale.github.io/courses/network-softwarization/pandas_tutorial/</link>
<pubDate>Mon, 19 Sep 2022 00:00:00 +0000</pubDate>
<guid>https://lorenzobracciale.github.io/courses/network-softwarization/pandas_tutorial/</guid>
<description><p><em>Course of Network Softwarization</em></p>
<p><em>Machine Learning for Networking</em></p>
<p><em>University of Rome &ldquo;Tor Vergata&rdquo;</em></p>
<p>Lorenzo Bracciale</p>
<h1 id="data-manipulation-with-pandas">Data manipulation with pandas</h1>
<p>This is a short tutorial on main Pandas functions. Please refer to the <a href="https://pandas.pydata.org/docs/" target="_blank" rel="noopener">official website</a> for a more in-depth guide on Pandas.</p>
<p><strong>credits</strong>: most of the material has been taken by the following tutorials.</p>
<p><a href="https://www.kaggle.com/learn/pandas" target="_blank" rel="noopener">Kaggle tutorial</a></p>
<p><a href="https://pandas.pydata.org/docs/user_guide/10min.html" target="_blank" rel="noopener">10 minutes to pandas</a></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
</span></span></code></pre></div><h2 id="creating-dataframes">Creating dataframes</h2>
<p>The most important data structure in Pandas is the <strong>DataFrame</strong> which essentially is a table.</p>
<p>Let us create our first dataframe</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&#39;A&#39;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="s1">&#39;B&#39;</span><span class="p">:</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">]})</span>
</span></span></code></pre></div><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
<pre><code>.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</code></pre>
<p></style></p>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>1</td>
<td>3</td>
</tr>
<tr>
<th>1</th>
<td>2</td>
<td>4</td>
</tr>
</tbody>
</table>
</div>
<p>In this example, we set the names of the <em>columns</em> as &ldquo;A&rdquo; and &ldquo;B&rdquo;.</p>
<p>The names of the <em>rows</em>, in this example &ldquo;0&rdquo; and &ldquo;1&rdquo;, have been assigned by default and are called &ldquo;indexes&rdquo;.</p>
<p>We can explicitly specify the index of the dataframe in this way:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&#39;A&#39;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="s1">&#39;B&#39;</span><span class="p">:</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">]},</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;X&#39;</span><span class="p">,</span> <span class="s1">&#39;Y&#39;</span><span class="p">])</span>
</span></span></code></pre></div><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
<pre><code>.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</code></pre>
<p></style></p>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
</tr>
</thead>
<tbody>
<tr>
<th>X</th>
<td>1</td>
<td>3</td>
</tr>
<tr>
<th>Y</th>
<td>2</td>
<td>4</td>
</tr>
</tbody>
</table>
</div>
<p>There exists also another data structure called <strong>Series</strong>, which is essentially a list, or we can see it as a column of a table.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">])</span> <span class="c1"># with automatic indexes</span>
</span></span></code></pre></div><pre><code>0 1
1 2
dtype: int64
</code></pre>
<p>Like in the dataframe, we can specify the index of the Series as well. Moreover, we can also specify the name of the Series.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;A&#39;</span><span class="p">,</span> <span class="s1">&#39;B&#39;</span><span class="p">],</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;Product A&#39;</span><span class="p">)</span> <span class="c1"># with manual indexes and a name</span>
</span></span></code></pre></div><pre><code>A 1
B 2
Name: Product A, dtype: int64
</code></pre>
<p>Each Series has a data type (dtype). In the example above it was <code>int64</code>, but we can decide to create a Series with categoritcal data such as the next one, which will be assigned to the dtype <code>object</code>.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s2">&#34;good&#34;</span><span class="p">,</span> <span class="s2">&#34;good&#34;</span><span class="p">,</span> <span class="s2">&#34;bad&#34;</span><span class="p">,</span> <span class="s2">&#34;good&#34;</span><span class="p">])</span>
</span></span></code></pre></div><pre><code>0 good
1 good
2 bad
3 good
dtype: object
</code></pre>
<p>We can access to values and indexes of Series and Dataset in this way:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&#39;A&#39;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="s1">&#39;B&#39;</span><span class="p">:</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">]})</span>
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="s2">&#34;Values of the dataframe&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">values</span><span class="p">)</span> <span class="c1"># this returns a numpy array</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="s2">&#34;</span><span class="se">\n</span><span class="s2">Index of the dataframe&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">index</span><span class="p">)</span>
</span></span></code></pre></div><pre><code>Values of the dataframe
[[1 3]
[2 4]]
Index of the dataframe
RangeIndex(start=0, stop=2, step=1)
</code></pre>
<p>Typically series and dataframes are big and you need to import them automatically from a file.</p>
<p>You can also load your dataset from many formats like CSV, json or Excel:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="c1">#pd.read_csv(&#39;/your/path/file.csv&#39;)</span>
</span></span><span class="line"><span class="cl"><span class="c1">#pd.read_excel(&#39;/your/path/file.xlsx&#39;)</span>
</span></span><span class="line"><span class="cl"><span class="c1">#pd.read_json(&#39;/your/path/file.json&#39;)</span>
</span></span></code></pre></div><h2 id="indexing-and-selecting-data">Indexing and selecting data</h2>
<p>Let us start by creating a simple testing dataframe to play with, and assign it to the variable <code>df</code></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&#39;A&#39;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">],</span> <span class="s1">&#39;B&#39;</span><span class="p">:</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">]},</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;X&#39;</span><span class="p">,</span> <span class="s1">&#39;Y&#39;</span><span class="p">,</span> <span class="s1">&#39;Z&#39;</span><span class="p">])</span>
</span></span></code></pre></div><p>To view only the first lines (5 by default) we can use the <code>head</code> method. Likewise, to see the last lines of the dataframe we could use the <code>tail</code> method.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</span></span></code></pre></div><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
<pre><code>.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</code></pre>
<p></style></p>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
</tr>
</thead>
<tbody>
<tr>
<th>X</th>
<td>1</td>
<td>4</td>
</tr>
<tr>
<th>Y</th>
<td>2</td>
<td>5</td>
</tr>
<tr>
<th>Z</th>
<td>3</td>
<td>6</td>
</tr>
</tbody>
</table>
</div>
<p>When we do machine learning, it is very important to understand the dimension of the dataset which is readily provided by the <code>shape</code> attribute:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="o">.</span><span class="n">shape</span>
</span></span></code></pre></div><pre><code>(3, 2)
</code></pre>
<p>We can then access to a specific <em>column</em> as</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="p">[</span><span class="s1">&#39;A&#39;</span><span class="p">]</span> <span class="c1"># &lt;-- this returns a Series</span>
</span></span></code></pre></div><pre><code>X 1
Y 2
Z 3
Name: A, dtype: int64
</code></pre>
<p>or using the next method</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="o">.</span><span class="n">A</span>
</span></span></code></pre></div><pre><code>X 1
Y 2
Z 3
Name: A, dtype: int64
</code></pre>
<p>We can get the value of a specific <em>cell</em> in this way:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="p">[</span><span class="s1">&#39;A&#39;</span><span class="p">][</span><span class="s1">&#39;X&#39;</span><span class="p">]</span> <span class="c1"># &lt;-- this returns a value</span>
</span></span></code></pre></div><pre><code>1
</code></pre>
<p>For more advanced indexing, we can resort to the following attributes:</p>
<ul>
<li><code>loc</code> : selection by <em>label</em></li>
<li><code>iloc</code> : selection by <em>position</em></li>
</ul>
<p>We can select our data based on its <em>numerical</em> position with <code>iloc</code>:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="s2">&#34;** First row **&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="s2">&#34;</span><span class="se">\n</span><span class="s2"> ** First row, First column **&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">])</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="s2">&#34;</span><span class="se">\n</span><span class="s2">** Second column **&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">iloc</span><span class="p">[:,</span><span class="mi">1</span><span class="p">])</span>
</span></span></code></pre></div><pre><code>** First row **
A 1
B 4
Name: X, dtype: int64
** First row, First column **
1
** Second column **
X 4
Y 5
Z 6
Name: B, dtype: int64
</code></pre>
<p>Or use <em>label-based</em> selection with <code>loc</code>:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">&#39;X&#39;</span><span class="p">,</span> <span class="s1">&#39;A&#39;</span><span class="p">]</span>
</span></span></code></pre></div><pre><code>1
</code></pre>
<p>We create expressions such as the following one. Please note that the result is a <em>column</em> of boolean values</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="p">[</span><span class="s1">&#39;A&#39;</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">1</span>
</span></span></code></pre></div><pre><code>X False
Y True
Z True
Name: A, dtype: bool
</code></pre>
<p>We can then use this column to select only a <em>subset</em> of our dataset.</p>
<p>For instance, with the next command we select only the rows where the value of the &ldquo;A&rdquo; column is greater than 1.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="p">[</span><span class="n">df</span><span class="p">[</span><span class="s1">&#39;A&#39;</span><span class="p">]</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">]</span> <span class="c1"># only the second raw displayed</span>
</span></span></code></pre></div><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
<pre><code>.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</code></pre>
<p></style></p>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
</tr>
</thead>
<tbody>
<tr>
<th>Y</th>
<td>2</td>
<td>5</td>
</tr>
<tr>
<th>Z</th>
<td>3</td>
<td>6</td>
</tr>
</tbody>
</table>
</div>
<p>Another usefull selection function is <code>isin</code> to check if the values are inside a given list</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="p">[</span><span class="s1">&#39;A&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">isin</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">9</span><span class="p">])</span>
</span></span></code></pre></div><pre><code>X True
Y False
Z False
Name: A, dtype: bool
</code></pre>
<p>Finally we will add a new column to an existing dataset.</p>
<p>To add (or replace) a column with <em>constant</em> value you can simple make the new column equal to a <em>single</em> value</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="p">[</span><span class="s1">&#39;D&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
</span></span><span class="line"><span class="cl"><span class="n">df</span>
</span></span></code></pre></div><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
<pre><code>.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</code></pre>
<p></style></p>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<th>X</th>
<td>1</td>
<td>4</td>
<td>0</td>
</tr>
<tr>
<th>Y</th>
<td>2</td>
<td>5</td>
<td>0</td>
</tr>
<tr>
<th>Z</th>
<td>3</td>
<td>6</td>
<td>0</td>
</tr>
</tbody>
</table>
</div>
<p>Conversely, if you want to provide all the values of the new column, you can write:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
</span></span><span class="line"><span class="cl"><span class="n">df</span><span class="p">[</span><span class="s1">&#39;C&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="c1"># equals to [0, 1, 2]</span>
</span></span><span class="line"><span class="cl"><span class="n">df</span><span class="p">[</span><span class="s1">&#39;D&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;Good&#39;</span><span class="p">,</span> <span class="s1">&#39;Bad&#39;</span><span class="p">,</span> <span class="s1">&#39;Bad&#39;</span><span class="p">]</span> <span class="c1">#categorical data</span>
</span></span><span class="line"><span class="cl"><span class="n">df</span>
</span></span></code></pre></div><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
<pre><code>.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</code></pre>
<p></style></p>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>D</th>
<th>C</th>
</tr>
</thead>
<tbody>
<tr>
<th>X</th>
<td>1</td>
<td>4</td>
<td>Good</td>
<td>0</td>
</tr>
<tr>
<th>Y</th>
<td>2</td>
<td>5</td>
<td>Bad</td>
<td>1</td>
</tr>
<tr>
<th>Z</th>
<td>3</td>
<td>6</td>
<td>Bad</td>
<td>2</td>
</tr>
</tbody>
</table>
</div>
<p># Usefull methods</p>
<p>Pandas is full of usefull methods to understand what is going on with your data.</p>
<p>For instance, to show some statistics about the current dataset we can use <code>describe</code></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="o">.</span><span class="n">describe</span><span class="p">()</span>
</span></span></code></pre></div><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
<pre><code>.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</code></pre>
<p></style></p>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
</thead>
<tbody>
<tr>
<th>count</th>
<td>3.0</td>
<td>3.0</td>
<td>3.0</td>
</tr>
<tr>
<th>mean</th>
<td>2.0</td>
<td>5.0</td>
<td>1.0</td>
</tr>
<tr>
<th>std</th>
<td>1.0</td>
<td>1.0</td>
<td>1.0</td>
</tr>
<tr>
<th>min</th>
<td>1.0</td>
<td>4.0</td>
<td>0.0</td>
</tr>
<tr>
<th>25%</th>
<td>1.5</td>
<td>4.5</td>
<td>0.5</td>
</tr>
<tr>
<th>50%</th>
<td>2.0</td>
<td>5.0</td>
<td>1.0</td>
</tr>
<tr>
<th>75%</th>
<td>2.5</td>
<td>5.5</td>
<td>1.5</td>
</tr>
<tr>
<th>max</th>
<td>3.0</td>
<td>6.0</td>
<td>2.0</td>
</tr>
</tbody>
</table>
</div>
<p>We can call different statistical methods on any given column such as <em>mean</em>, <em>std</em>, <em>min</em> or <em>max</em>. For instance:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="p">[</span><span class="s1">&#39;A&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
</span></span></code></pre></div><pre><code>2.0
</code></pre>
<p><em>Unique</em> returns the value set of a columns. For instance, the set of unique values of column &ldquo;D&rdquo; are &ldquo;Good&rdquo; or &ldquo;Bad&rdquo;:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="p">[</span><span class="s1">&#39;D&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">unique</span><span class="p">()</span>
</span></span></code></pre></div><pre><code>array(['Good', 'Bad'], dtype=object)
</code></pre>
<p><code>value_counts</code> is also very used since it give us the occurences of all the values:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="p">[</span><span class="s1">&#39;D&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">value_counts</span><span class="p">()</span> <span class="c1">#Two bad elements, good just one element</span>
</span></span></code></pre></div><pre><code>Bad 2
Good 1
Name: D, dtype: int64
</code></pre>
<p>## Modify values</p>
<p>It is common to apply a certain function to all the values of a column.</p>
<p>This is easily done with the <code>apply</code> method. For instance we could want to make a square out of a column, like this:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="p">[</span><span class="s1">&#39;C&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">p</span><span class="p">:</span> <span class="n">p</span> <span class="o">*</span> <span class="n">p</span><span class="p">)</span>
</span></span></code></pre></div><pre><code>X 0
Y 1
Z 4
Name: C, dtype: int64
</code></pre>
<p>Please note that most of these functions <em>do not modify</em> the original dataset!</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="p">[</span><span class="s1">&#39;C&#39;</span><span class="p">]</span> <span class="c1"># it is not changed!</span>
</span></span></code></pre></div><pre><code>X 0
Y 1
Z 2
Name: C, dtype: int64
</code></pre>
<p>To actually change the column we can assign the modified dataframe such as:
<code>df['C'] = df['C'].map(lambda p: p * p) </code></p>
<p>We can also call <code>apply</code> on each <em>row</em>. We are going to experiment with only the numerical part of our dataset (first three columns) since it would raise an exception if we launch it on a categorical column such as column &ldquo;D&rdquo;.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="p">[[</span><span class="s1">&#39;A&#39;</span><span class="p">,</span> <span class="s1">&#39;B&#39;</span><span class="p">,</span> <span class="s1">&#39;C&#39;</span><span class="p">]]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">p</span><span class="p">:</span> <span class="n">p</span> <span class="o">*</span> <span class="n">p</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="s1">&#39;columns&#39;</span><span class="p">)</span>
</span></span></code></pre></div><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
<pre><code>.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</code></pre>
<p></style></p>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
</thead>
<tbody>
<tr>
<th>X</th>
<td>1</td>
<td>16</td>
<td>0</td>
</tr>
<tr>
<th>Y</th>
<td>4</td>
<td>25</td>
<td>1</td>
</tr>
<tr>
<th>Z</th>
<td>9</td>
<td>36</td>
<td>4</td>
</tr>
</tbody>
</table>
</div>
<p>## Grouping</p>
<p>We can group our rows and then performing some calculation (e.g., <em>count</em> or <em>min</em>) on the fields:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="s1">&#39;D&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">count</span><span class="p">()</span>
</span></span></code></pre></div><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
<pre><code>.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</code></pre>
<p></style></p>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
<tr>
<th>D</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th>Bad</th>
<td>2</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<th>Good</th>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
</tbody>
</table>
</div>
<h2 id="data-types">Data types</h2>
<p>All the columns of our dataframe is a Series, and each Series has a data type (<em>dtype</em>)</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="p">[</span><span class="s1">&#39;A&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">dtype</span>
</span></span></code></pre></div><pre><code>dtype('int64')
</code></pre>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="o">.</span><span class="n">dtypes</span> <span class="c1"># to watch all the dtypes</span>
</span></span></code></pre></div><pre><code>A int64
B int64
D object
C int64
dtype: object
</code></pre>
<p>We can change the datatype with astype. For instance:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="p">[</span><span class="s1">&#39;A&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">&#39;float64&#39;</span><span class="p">)</span>
</span></span></code></pre></div><pre><code>X 1.0
Y 2.0
Z 3.0
Name: A, dtype: float64
</code></pre>
<p>When we import a csv in pandas, it automatically try to guess the right datatype. Most of the time it does a very good job, however sometimes it can be usefull to force the <code>dtype</code> on some column. Please refer to the <a href="https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html" target="_blank" rel="noopener">official docs</a> of pandas to know more.</p>
<p>## Not a Number</p>
<p>It is common to <em>do not</em> have all the data.</p>
<p>In such cases, pandas fills the missing values with <code>Not a Number</code> value, aka <code>NaN</code>.</p>
<p>We are going to simulate this case:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="p">[</span><span class="s2">&#34;E&#34;</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="mi">1</span> <span class="c1">#simulate a columns with two missing values</span>
</span></span><span class="line"><span class="cl"><span class="n">df</span>
</span></span></code></pre></div><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
<pre><code>.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</code></pre>
<p></style></p>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>D</th>
<th>C</th>
<th>E</th>
</tr>
</thead>
<tbody>
<tr>
<th>X</th>
<td>1</td>
<td>4</td>
<td>Good</td>
<td>0</td>
<td>NaN</td>
</tr>
<tr>
<th>Y</th>
<td>2</td>
<td>5</td>
<td>Bad</td>
<td>1</td>
<td>NaN</td>
</tr>
<tr>
<th>Z</th>
<td>3</td>
<td>6</td>
<td>Bad</td>
<td>2</td>
<td>1.0</td>
</tr>
</tbody>
</table>
</div>
<p><code>isnull</code> and <code>notnull</code> are two usefull selectors for such null values</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">isnull</span><span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="s1">&#39;E&#39;</span><span class="p">]))</span>
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="s2">&#34;*********&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">notnull</span><span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="s1">&#39;E&#39;</span><span class="p">]))</span>
</span></span></code></pre></div><pre><code>X True
Y True
Z False
Name: E, dtype: bool
*********
X False
Y False
Z True
Name: E, dtype: bool
</code></pre>
<p>How to do machine learning with <code>NaN</code>?</p>
<p>It is not uncommon we need to substitute a constant value in place of all the <code>NaN</code>.
This is readily done by the <code>fillna</code> method.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="o">.</span><span class="n">fillna</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
</span></span></code></pre></div><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
<pre><code>.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</code></pre>
<p></style></p>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>D</th>
<th>C</th>
<th>E</th>
</tr>
</thead>
<tbody>
<tr>
<th>X</th>
<td>1</td>
<td>4</td>
<td>Good</td>
<td>0</td>
<td>42.0</td>
</tr>
<tr>
<th>Y</th>
<td>2</td>
<td>5</td>
<td>Bad</td>
<td>1</td>
<td>42.0</td>
</tr>
<tr>
<th>Z</th>
<td>3</td>
<td>6</td>
<td>Bad</td>
<td>2</td>
<td>1.0</td>
</tr>
</tbody>
</table>
</div>
<p>Or we can decide to drop the lines with `NaN``</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df</span><span class="o">.</span><span class="n">dropna</span><span class="p">()</span>
</span></span></code></pre></div><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
<pre><code>.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</code></pre>
<p></style></p>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>D</th>
<th>C</th>
<th>E</th>
</tr>
</thead>
<tbody>
<tr>
<th>Z</th>
<td>3</td>
<td>6</td>
<td>Bad</td>
<td>2</td>
<td>1.0</td>
</tr>
</tbody>
</table>
</div>
<h2 id="concat-join-merge-split">Concat, join, merge, split</h2>
<p><em>Concat</em> concatenates pandas objects along a particular axis (rows or columns)</p>
<p>
<figure >
<div class="d-flex justify-content-center">
<div class="w-100" ><img src="https://pandas.pydata.org/docs/_images/merging_concat_basic.png" alt="cat" loading="lazy" data-zoomable /></div>
</div></figure>
(credtis <a href="https://pandas.pydata.org/" target="_blank" rel="noopener">https://pandas.pydata.org/</a>)</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&#39;A&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;AO&#39;</span><span class="p">,</span> <span class="s1">&#39;A1&#39;</span><span class="p">],</span> <span class="s1">&#39;B&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;B0&#39;</span><span class="p">,</span> <span class="s1">&#39;B1&#39;</span><span class="p">],</span> <span class="s1">&#39;C&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;C0&#39;</span><span class="p">,</span> <span class="s1">&#39;C1&#39;</span><span class="p">]})</span>
</span></span><span class="line"><span class="cl"><span class="n">df2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&#39;A&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;A2&#39;</span><span class="p">,</span> <span class="s1">&#39;A3&#39;</span><span class="p">],</span> <span class="s1">&#39;B&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;B2&#39;</span><span class="p">,</span> <span class="s1">&#39;B3&#39;</span><span class="p">],</span> <span class="s1">&#39;C&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;C2&#39;</span><span class="p">,</span> <span class="s1">&#39;C3&#39;</span><span class="p">]})</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="s2">&#34;DataFrame1&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="n">df1</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="s2">&#34;</span><span class="se">\n</span><span class="s2">DataFrame2&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="n">df2</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="s2">&#34;</span><span class="se">\n</span><span class="s2">Concat of DF1 and DF2&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">df1</span><span class="p">,</span> <span class="n">df2</span><span class="p">]))</span>
</span></span></code></pre></div><pre><code>DataFrame1
A B C
0 AO B0 C0
1 A1 B1 C1
DataFrame2
A B C
0 A2 B2 C2
1 A3 B3 C3
Concat of DF1 and DF2
A B C
0 AO B0 C0
1 A1 B1 C1
0 A2 B2 C2
1 A3 B3 C3
</code></pre>
<p><code>concat</code> can be done on rows (by default, as in the example above), or on columns.</p>
<p>As happens for many Pandas commands, we can specify that with the <code>axis</code> parameter, where:</p>
<ul>
<li><code>axis=0</code>: means &ldquo;do that for rows&rdquo;</li>
<li><code>axis=1</code>: means &ldquo;do that for columns&rdquo;</li>
</ul>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="n">df1</span><span class="p">,</span> <span class="n">df2</span><span class="p">],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span> <span class="c1">#concat the columns</span>
</span></span></code></pre></div><pre><code> A B C A B C
0 AO B0 C0 A2 B2 C2
1 A1 B1 C1 A3 B3 C3
</code></pre>
<p><code>merge</code> is more complicated since there are many forms of join:</p>
<ul>
<li><em>inner join</em>: It returns a dataframe with only those rows that are on both dataframes. This is similar to the intersection of two sets.</li>
<li><em>outer join</em>: returns all those records which either have a match in the left or right dataframe.</li>
<li><em>left join</em>: returns a dataframe containing all the rows of the left dataframe. All the non-matching rows of the left dataframe contain NaN for the columns in the right dataframe.</li>
<li><em>right join</em>: same as left join but clearly on right dataframe!</li>
</ul>
<p>Using merge with default arguments results in an inner join.</p>
<p>
<figure >
<div class="d-flex justify-content-center">
<div class="w-100" ><img src="https://lorenzobracciale.github.io/merge_types.png" alt="./merge_types.png" loading="lazy" data-zoomable /></div>
</div></figure>
</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">df1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&#39;a&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="s1">&#39;bar&#39;</span><span class="p">],</span> <span class="s1">&#39;b&#39;</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]})</span>
</span></span><span class="line"><span class="cl"><span class="n">df2</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">&#39;a&#39;</span><span class="p">:</span> <span class="p">[</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="s1">&#39;baz&#39;</span><span class="p">],</span> <span class="s1">&#39;c&#39;</span><span class="p">:</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">]})</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="s2">&#34;DataFrame1&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="n">df1</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="s2">&#34;</span><span class="se">\n</span><span class="s2">DataFrame2&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="n">df2</span><span class="p">)</span>
</span></span></code></pre></div><pre><code>DataFrame1
a b
0 foo 1
1 bar 2
DataFrame2
a c
0 foo 3
1 baz 4
</code></pre>
<p><strong>Left merge</strong></p>
<p>We are merging on the column &ldquo;a&rdquo;.</p>
<p>Given that we are left merging, we will have all the rows of <code>df1</code></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df1</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="n">df2</span><span class="p">,</span> <span class="n">how</span><span class="o">=</span><span class="s1">&#39;left&#39;</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="s1">&#39;a&#39;</span><span class="p">)</span>
</span></span></code></pre></div><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
<pre><code>.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</code></pre>
<p></style></p>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>a</th>
<th>b</th>
<th>c</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>foo</td>
<td>1</td>
<td>3.0</td>
</tr>
<tr>
<th>1</th>
<td>bar</td>
<td>2</td>
<td>NaN</td>
</tr>
</tbody>
</table>
</div>
<p><strong>Right merge</strong></p>
<p>We are merging on the column &ldquo;a&rdquo;.</p>
<p>Given that we are left merging, we will have all the rows of <code>df2</code></p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="n">df1</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="n">df2</span><span class="p">,</span> <span class="n">how</span><span class="o">=</span><span class="s1">&#39;right&#39;</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="s1">&#39;a&#39;</span><span class="p">)</span>
</span></span></code></pre></div><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
<pre><code>.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</code></pre>
<p></style></p>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>