-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clase19__.html
2112 lines (2006 loc) · 70.3 KB
/
Clase19__.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" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Clase 19: Loops, funciones y SQL — Introducción a python</title>
<link rel="stylesheet" href="_static/css/index.f658d18f9b420779cfdf24aa0a7e2d77.css">
<link rel="stylesheet"
href="_static/vendor/fontawesome/5.13.0/css/all.min.css">
<link rel="preload" as="font" type="font/woff2" crossorigin
href="_static/vendor/fontawesome/5.13.0/webfonts/fa-solid-900.woff2">
<link rel="preload" as="font" type="font/woff2" crossorigin
href="_static/vendor/fontawesome/5.13.0/webfonts/fa-brands-400.woff2">
<link rel="stylesheet"
href="_static/vendor/open-sans_all/1.44.1/index.css">
<link rel="stylesheet"
href="_static/vendor/lato_latin-ext/1.44.1/index.css">
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/sphinx-book-theme.e7340bb3dbd8dde6db86f25597f54a1b.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="_static/togglebutton.css" />
<link rel="stylesheet" type="text/css" href="_static/copybutton.css" />
<link rel="stylesheet" type="text/css" href="_static/mystnb.css" />
<link rel="stylesheet" type="text/css" href="_static/sphinx-thebe.css" />
<link rel="stylesheet" type="text/css" href="_static/panels-main.c949a650a448cc0ae9fd3441c0e17fb0.css" />
<link rel="stylesheet" type="text/css" href="_static/panels-variables.06eb56fa6e07937060861dad626602ad.css" />
<link rel="preload" as="script" href="_static/js/index.d3f166471bb80abb5163.js">
<script id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
<script src="_static/jquery.js"></script>
<script src="_static/underscore.js"></script>
<script src="_static/doctools.js"></script>
<script src="_static/togglebutton.js"></script>
<script src="_static/clipboard.min.js"></script>
<script src="_static/copybutton.js"></script>
<script >var togglebuttonSelector = '.toggle, .admonition.dropdown, .tag_hide_input div.cell_input, .tag_hide-input div.cell_input, .tag_hide_output div.cell_output, .tag_hide-output div.cell_output, .tag_hide_cell.cell, .tag_hide-cell.cell';</script>
<script src="_static/sphinx-book-theme.7d483ff0a819d6edff12ce0b1ead3928.js"></script>
<script async="async" src="https://unpkg.com/thebelab@latest/lib/index.js"></script>
<script >
const thebe_selector = ".thebe"
const thebe_selector_input = "pre"
const thebe_selector_output = ".output"
</script>
<script async="async" src="_static/sphinx-thebe.js"></script>
<script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]], "processRefs": false, "processEnvironments": false}})</script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="docsearch:language" content="en" />
</head>
<body data-spy="scroll" data-target="#bd-toc-nav" data-offset="80">
<div class="container-xl">
<div class="row">
<div class="col-12 col-md-3 bd-sidebar site-navigation show" id="site-navigation">
<div class="navbar-brand-box">
<a class="navbar-brand text-wrap" href="index.html">
<img src="_static/logo_fae.png" class="logo" alt="logo">
<h1 class="site-logo" id="site-title">Introducción a python</h1>
</a>
</div><form class="bd-search d-flex align-items-center" action="search.html" method="get">
<i class="icon fas fa-search"></i>
<input type="search" class="form-control" name="q" id="search-input" placeholder="Search this book..." aria-label="Search this book..." autocomplete="off" >
</form>
<nav class="bd-links" id="bd-docs-nav" aria-label="Main navigation">
<ul class="nav sidenav_l1">
<li class="toctree-l1">
<a class="reference internal" href="intro.html">
Python para Ingeniería Comercial
</a>
</li>
</ul>
<ul class="nav sidenav_l1">
<li class="toctree-l1">
<a class="reference internal" href="About_python.html">
Clase 2: About Python
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="clase3_4.html">
Clase 3-4: lo básico
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="Clase5_6.html">
Clase 5-6: lo básico (continuación)
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="Clase5_6.html#actividad">
Actividad
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="Clase7_8.html">
Clase 7-8
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="Clase9_10.html">
Clase 9-10: Funciones, diccionarios y condicionales
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="Clase9_10.html#actividad-1">
Actividad 1
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="Clase11_12.html">
Clase 11-12: Introducción a los gráficos
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="Clase_13_14_a.html">
Clase 13: introducción a los datos
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="Clase_13_14_b.html">
Clase 14: continuación con los datos
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="Clase15.html">
Clase 15: gráficos
<em>
pro
</em>
con
<code class="docutils literal notranslate">
<span class="pre">
Plotly
</span>
</code>
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="Clase15.html#actividad">
Actividad
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="Clase17.html">
Clase 17: Introducción al análisis de texto (text mining)
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="Clase18.html">
Clase 18: Introducción al uso de clases
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="Clase19.html">
Clase 19: Scipy y computación numérica
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="Clase20.html">
Clase 20: Regresiones
</a>
</li>
</ul>
</nav> <!-- To handle the deprecated key -->
<div class="navbar_extra_footer">
Powered by <a href="https://jupyterbook.org">Jupyter Book</a>
</div>
</div>
<main class="col py-md-3 pl-md-4 bd-content overflow-auto" role="main">
<div class="topbar container-xl fixed-top">
<div class="topbar-contents row">
<div class="col-12 col-md-3 bd-topbar-whitespace site-navigation show"></div>
<div class="col pl-md-4 topbar-main">
<button id="navbar-toggler" class="navbar-toggler ml-0" type="button" data-toggle="collapse"
data-toggle="tooltip" data-placement="bottom" data-target=".site-navigation" aria-controls="navbar-menu"
aria-expanded="true" aria-label="Toggle navigation" aria-controls="site-navigation"
title="Toggle navigation" data-toggle="tooltip" data-placement="left">
<i class="fas fa-bars"></i>
<i class="fas fa-arrow-left"></i>
<i class="fas fa-arrow-up"></i>
</button>
<div class="dropdown-buttons-trigger">
<button id="dropdown-buttons-trigger" class="btn btn-secondary topbarbtn" aria-label="Download this page"><i
class="fas fa-download"></i></button>
<div class="dropdown-buttons">
<!-- ipynb file if we had a myst markdown file -->
<!-- Download raw file -->
<a class="dropdown-buttons" href="_sources/Clase19__.ipynb"><button type="button"
class="btn btn-secondary topbarbtn" title="Download source file" data-toggle="tooltip"
data-placement="left">.ipynb</button></a>
<!-- Download PDF via print -->
<button type="button" id="download-print" class="btn btn-secondary topbarbtn" title="Print to PDF"
onClick="window.print()" data-toggle="tooltip" data-placement="left">.pdf</button>
</div>
</div>
<!-- Source interaction buttons -->
<div class="dropdown-buttons-trigger">
<button id="dropdown-buttons-trigger" class="btn btn-secondary topbarbtn"
aria-label="Connect with source repository"><i class="fab fa-github"></i></button>
<div class="dropdown-buttons sourcebuttons">
<a class="repository-button"
href="https://github.com/executablebooks/jupyter-book"><button type="button" class="btn btn-secondary topbarbtn"
data-toggle="tooltip" data-placement="left" title="Source repository"><i
class="fab fa-github"></i>repository</button></a>
<a class="issues-button"
href="https://github.com/executablebooks/jupyter-book/issues/new?title=Issue%20on%20page%20%2FClase19__.html&body=Your%20issue%20content%20here."><button
type="button" class="btn btn-secondary topbarbtn" data-toggle="tooltip" data-placement="left"
title="Open an issue"><i class="fas fa-lightbulb"></i>open issue</button></a>
</div>
</div>
<!-- Full screen (wrap in <a> to have style consistency -->
<a class="full-screen-button"><button type="button" class="btn btn-secondary topbarbtn" data-toggle="tooltip"
data-placement="bottom" onclick="toggleFullScreen()" aria-label="Fullscreen mode"
title="Fullscreen mode"><i
class="fas fa-expand"></i></button></a>
<!-- Launch buttons -->
<div class="dropdown-buttons-trigger">
<button id="dropdown-buttons-trigger" class="btn btn-secondary topbarbtn"
aria-label="Launch interactive content"><i class="fas fa-rocket"></i></button>
<div class="dropdown-buttons">
<a class="binder-button" href="https://mybinder.org/v2/gh/executablebooks/jupyter-book/master?urlpath=tree/docs/Clase19__.ipynb"><button type="button"
class="btn btn-secondary topbarbtn" title="Launch Binder" data-toggle="tooltip"
data-placement="left"><img class="binder-button-logo"
src="_static/images/logo_binder.svg"
alt="Interact on binder">Binder</button></a>
</div>
</div>
</div>
<!-- Table of contents -->
<div class="d-none d-md-block col-md-2 bd-toc show">
<div class="tocsection onthispage pt-5 pb-3">
<i class="fas fa-list"></i>
Contents
</div>
<nav id="bd-toc-nav">
<ul class="nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#actividad-1">
Actividad 1:
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#actividad-2">
Actividad 2
</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
<div id="main-content" class="row">
<div class="col-12 col-md-9 pl-md-3 pr-md-0">
<div>
<div class="section" id="clase-19-loops-funciones-y-sql">
<h1>Clase 19: Loops, funciones y SQL<a class="headerlink" href="#clase-19-loops-funciones-y-sql" title="Permalink to this headline">¶</a></h1>
<p>En esta sesión revisaremos algunos conceptos vistos previamente como loops, funciones, listas, diccionarios, elementos de pandas y gráficos. Vamos a resolver dos aplicaciones en base a estos contenidos.</p>
<div class="section" id="actividad-1">
<h2>Actividad 1:<a class="headerlink" href="#actividad-1" title="Permalink to this headline">¶</a></h2>
<p>Usando los datos de desempleados, ocupados e informales realizar lo siguiente:</p>
<ol class="simple">
<li><p>Importar las bases. Revisar si tienen características similares.</p></li>
<li><p>Juntar las bases usando algún tipo de join. Mostrar qué dan según los diferentes tipos y elegir el adecuado.</p></li>
<li><p>Mostrar el promedio de desocupados y ocupados por año. Guardar en un nuevo DataFrame.</p></li>
<li><p>En el DataFrame del punto 2, agregar una columna con la tasa de desempleo (desocupados/(desocupados+ocupados)) y otra columna para la tasa de informalidad (informales/ocupados).</p></li>
<li><p>Agregar una columna para mostrar la diferencia y otra para la diferencia porcentual de ocupados por periodo.</p></li>
<li><p>Vamos a crear una función que evalúe si los ocupados están bajo o sobre la media.</p></li>
<li><p>Usando el DataFrame anterior, crear un nuevo set de datos que tenga sólo la llave, ocupados y ocup_cond cuando es verdadero.</p></li>
<li><p>Usando for e if mostrar si la tasa de ocupación está bajo o sobre el promedio.</p></li>
</ol>
<ol class="simple">
<li><p>Importar las bases. Revisar si tienen características similares.</p></li>
</ol>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="n">df_des</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_excel</span><span class="p">(</span><span class="s1">'/home/felix/Dropbox/Computational_Economics/Intro_python/Data_SQL/Desocupados.ods'</span><span class="p">,</span> <span class="p">)</span>
<span class="c1"># df_des.dtypes</span>
<span class="n">df_ocu</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_excel</span><span class="p">(</span><span class="s1">'/home/felix/Dropbox/Computational_Economics/Intro_python/Data_SQL/Ocupados.ods'</span><span class="p">,</span> <span class="p">)</span>
<span class="c1"># df_ocu.dtypes</span>
<span class="n">df_inf</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_excel</span><span class="p">(</span><span class="s1">'/home/felix/Dropbox/Computational_Economics/Intro_python/Data_SQL/Informal.ods'</span><span class="p">,</span> <span class="p">)</span>
<span class="n">df_inf</span><span class="o">.</span><span class="n">dtypes</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Año int64
Trimestre object
Total_formal float64
Total_informal float64
Hombres_formal float64
Hombres_informal float64
Mujeres_formal float64
Mujeres_informal float64
dtype: object
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">df_des</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_html"><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Año</th>
<th>Trimestre</th>
<th>Desocupados (Total)</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>2011</td>
<td>Dic - Feb</td>
<td>612.826981</td>
</tr>
<tr>
<th>1</th>
<td>2011</td>
<td>Ene - Mar</td>
<td>622.445409</td>
</tr>
<tr>
<th>2</th>
<td>2011</td>
<td>Feb - Abr</td>
<td>594.917705</td>
</tr>
<tr>
<th>3</th>
<td>2011</td>
<td>Mar - May</td>
<td>604.074497</td>
</tr>
<tr>
<th>4</th>
<td>2011</td>
<td>Abr - Jun</td>
<td>598.396488</td>
</tr>
</tbody>
</table>
</div></div></div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">df_ocu</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_html"><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Año</th>
<th>Trimestre</th>
<th>Ocupados (Total)</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>2010</td>
<td>Ene - Mar</td>
<td>7156.211576</td>
</tr>
<tr>
<th>1</th>
<td>2010</td>
<td>Feb - Abr</td>
<td>7198.777387</td>
</tr>
<tr>
<th>2</th>
<td>2010</td>
<td>Mar - May</td>
<td>7181.902888</td>
</tr>
<tr>
<th>3</th>
<td>2010</td>
<td>Abr - Jun</td>
<td>7221.575499</td>
</tr>
<tr>
<th>4</th>
<td>2010</td>
<td>May -Jul</td>
<td>7256.515676</td>
</tr>
</tbody>
</table>
</div></div></div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">df_inf</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_html"><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Año</th>
<th>Trimestre</th>
<th>Total_formal</th>
<th>Total_informal</th>
<th>Hombres_formal</th>
<th>Hombres_informal</th>
<th>Mujeres_formal</th>
<th>Mujeres_informal</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>2017</td>
<td>Jul - Sep</td>
<td>6193.891633</td>
<td>2419.200879</td>
<td>3643.825292</td>
<td>1333.469417</td>
<td>2550.066341</td>
<td>1085.731462</td>
</tr>
<tr>
<th>1</th>
<td>2017</td>
<td>Ago - Oct</td>
<td>6168.590625</td>
<td>2454.112893</td>
<td>3639.764667</td>
<td>1354.010989</td>
<td>2528.825959</td>
<td>1100.101904</td>
</tr>
<tr>
<th>2</th>
<td>2017</td>
<td>Sep - Nov</td>
<td>6207.712916</td>
<td>2504.968544</td>
<td>3657.915007</td>
<td>1366.333110</td>
<td>2549.797909</td>
<td>1138.635433</td>
</tr>
<tr>
<th>3</th>
<td>2017</td>
<td>Oct - Dic</td>
<td>6239.135838</td>
<td>2529.531019</td>
<td>3680.942180</td>
<td>1376.881958</td>
<td>2558.193658</td>
<td>1152.649061</td>
</tr>
<tr>
<th>4</th>
<td>2017</td>
<td>Nov - Ene</td>
<td>6309.734180</td>
<td>2484.188455</td>
<td>3727.954628</td>
<td>1349.005126</td>
<td>2581.779552</td>
<td>1135.183329</td>
</tr>
</tbody>
</table>
</div></div></div>
</div>
<ol class="simple">
<li><p>Juntar las bases usando algún tipo de join. Mostrar qué dan según los diferentes tipos y elegir el adecuado.</p></li>
</ol>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">IPython.display</span> <span class="kn">import</span> <span class="n">Image</span>
<span class="n">Image</span><span class="p">(</span><span class="s2">"join_sql.png"</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/Clase19___8_0.png" src="_images/Clase19___8_0.png" />
</div>
</div>
<p>Primero vamos a crear una llave (año-trim), para esto vamos a usar un <code class="docutils literal notranslate"><span class="pre">left</span> <span class="pre">join</span></code>. Luego probamos <code class="docutils literal notranslate"><span class="pre">inner</span> <span class="pre">join</span></code> y <code class="docutils literal notranslate"><span class="pre">full</span> <span class="pre">join</span></code>.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1">#Traemos llave</span>
<span class="n">trim</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_excel</span><span class="p">(</span><span class="s1">'/home/felix/Dropbox/Computational_Economics/Intro_python/Data_SQL/trimestres.ods'</span><span class="p">,</span> <span class="p">)</span>
<span class="n">trim</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_html"><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Trim</th>
<th>mes</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>Dic - Feb</td>
<td>1</td>
</tr>
<tr>
<th>1</th>
<td>Ene - Mar</td>
<td>2</td>
</tr>
<tr>
<th>2</th>
<td>Feb - Abr</td>
<td>3</td>
</tr>
<tr>
<th>3</th>
<td>Mar - May</td>
<td>4</td>
</tr>
<tr>
<th>4</th>
<td>Abr - Jun</td>
<td>5</td>
</tr>
</tbody>
</table>
</div></div></div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1">#Left join: dejamos todo lo de df_des (izquierda) y agregamos el mes</span>
<span class="n">df_des</span> <span class="o">=</span> <span class="n">df_des</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="n">trim</span><span class="p">,</span> <span class="n">left_on</span><span class="o">=</span><span class="s2">"Trimestre"</span><span class="p">,</span> <span class="n">right_on</span><span class="o">=</span><span class="s2">"Trim"</span><span class="p">,</span> <span class="n">how</span><span class="o">=</span><span class="s2">"left"</span><span class="p">)</span>
<span class="n">df_des</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_html"><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Año</th>
<th>Trimestre</th>
<th>Desocupados (Total)</th>
<th>Trim</th>
<th>mes</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>2011</td>
<td>Dic - Feb</td>
<td>612.826981</td>
<td>Dic - Feb</td>
<td>1</td>
</tr>
<tr>
<th>1</th>
<td>2011</td>
<td>Ene - Mar</td>
<td>622.445409</td>
<td>Ene - Mar</td>
<td>2</td>
</tr>
<tr>
<th>2</th>
<td>2011</td>
<td>Feb - Abr</td>
<td>594.917705</td>
<td>Feb - Abr</td>
<td>3</td>
</tr>
<tr>
<th>3</th>
<td>2011</td>
<td>Mar - May</td>
<td>604.074497</td>
<td>Mar - May</td>
<td>4</td>
</tr>
<tr>
<th>4</th>
<td>2011</td>
<td>Abr - Jun</td>
<td>598.396488</td>
<td>Abr - Jun</td>
<td>5</td>
</tr>
</tbody>
</table>
</div></div></div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1">#Crear llave: Desocupados</span>
<span class="n">df_des</span><span class="p">[</span><span class="s1">'llave'</span><span class="p">]</span> <span class="o">=</span> <span class="n">df_des</span><span class="p">[</span><span class="s2">"Año"</span><span class="p">]</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">str</span><span class="p">)</span> <span class="o">+</span> <span class="s2">"-"</span> <span class="o">+</span> <span class="n">df_des</span><span class="p">[</span><span class="s2">"mes"</span><span class="p">]</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">str</span><span class="p">)</span>
<span class="c1">#Limpiar columnas</span>
<span class="n">df_des</span> <span class="o">=</span> <span class="n">df_des</span><span class="o">.</span><span class="n">drop</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">'Trimestre'</span><span class="p">,</span> <span class="s1">'Trim'</span><span class="p">])</span>
<span class="n">df_des</span> <span class="o">=</span> <span class="n">df_des</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="p">{</span><span class="s2">"Desocupados (Total)"</span><span class="p">:</span> <span class="s2">"desoc"</span><span class="p">})</span>
<span class="n">df_des</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_html"><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Año</th>
<th>desoc</th>
<th>mes</th>
<th>llave</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>2011</td>
<td>612.826981</td>
<td>1</td>
<td>2011-1</td>
</tr>
<tr>
<th>1</th>
<td>2011</td>
<td>622.445409</td>
<td>2</td>
<td>2011-2</td>
</tr>
<tr>
<th>2</th>
<td>2011</td>
<td>594.917705</td>
<td>3</td>
<td>2011-3</td>
</tr>
<tr>
<th>3</th>
<td>2011</td>
<td>604.074497</td>
<td>4</td>
<td>2011-4</td>
</tr>
<tr>
<th>4</th>
<td>2011</td>
<td>598.396488</td>
<td>5</td>
<td>2011-5</td>
</tr>
</tbody>
</table>
</div></div></div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1">#Repetimos para las bases de ocupados y para informales</span>
<span class="sd">'''Ocupados'''</span>
<span class="n">df_ocu</span> <span class="o">=</span> <span class="n">df_ocu</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="n">trim</span><span class="p">,</span> <span class="n">left_on</span><span class="o">=</span><span class="s2">"Trimestre"</span><span class="p">,</span> <span class="n">right_on</span><span class="o">=</span><span class="s2">"Trim"</span><span class="p">,</span> <span class="n">how</span><span class="o">=</span><span class="s2">"left"</span><span class="p">)</span>
<span class="n">df_ocu</span><span class="p">[</span><span class="s1">'llave'</span><span class="p">]</span> <span class="o">=</span> <span class="n">df_ocu</span><span class="p">[</span><span class="s2">"Año"</span><span class="p">]</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">str</span><span class="p">)</span> <span class="o">+</span> <span class="s2">"-"</span> <span class="o">+</span> <span class="n">df_ocu</span><span class="p">[</span><span class="s2">"mes"</span><span class="p">]</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">str</span><span class="p">)</span>
<span class="n">df_ocu</span> <span class="o">=</span> <span class="n">df_ocu</span><span class="o">.</span><span class="n">drop</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">'Trimestre'</span><span class="p">,</span> <span class="s1">'Trim'</span><span class="p">])</span>
<span class="n">df_ocu</span> <span class="o">=</span> <span class="n">df_ocu</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="p">{</span><span class="s2">"Ocupados (Total)"</span><span class="p">:</span> <span class="s2">"ocupa"</span><span class="p">})</span>
<span class="sd">'''Informales'''</span>
<span class="n">df_inf</span> <span class="o">=</span> <span class="n">df_inf</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="n">trim</span><span class="p">,</span> <span class="n">left_on</span><span class="o">=</span><span class="s2">"Trimestre"</span><span class="p">,</span> <span class="n">right_on</span><span class="o">=</span><span class="s2">"Trim"</span><span class="p">,</span> <span class="n">how</span><span class="o">=</span><span class="s2">"left"</span><span class="p">)</span>
<span class="n">df_inf</span><span class="p">[</span><span class="s1">'llave'</span><span class="p">]</span> <span class="o">=</span> <span class="n">df_inf</span><span class="p">[</span><span class="s2">"Año"</span><span class="p">]</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">str</span><span class="p">)</span> <span class="o">+</span> <span class="s2">"-"</span> <span class="o">+</span> <span class="n">df_inf</span><span class="p">[</span><span class="s2">"mes"</span><span class="p">]</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">str</span><span class="p">)</span>
<span class="n">df_inf</span> <span class="o">=</span> <span class="n">df_inf</span><span class="o">.</span><span class="n">drop</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">'Trimestre'</span><span class="p">,</span> <span class="s1">'Trim'</span><span class="p">])</span>
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1">#Chequeamos que está ok</span>
<span class="n">df_ocu</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_html"><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Año</th>
<th>ocupa</th>
<th>mes</th>
<th>llave</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>2010</td>
<td>7156.211576</td>
<td>2</td>
<td>2010-2</td>
</tr>
<tr>
<th>1</th>
<td>2010</td>
<td>7198.777387</td>
<td>3</td>
<td>2010-3</td>
</tr>
<tr>
<th>2</th>
<td>2010</td>
<td>7181.902888</td>
<td>4</td>
<td>2010-4</td>
</tr>
<tr>
<th>3</th>
<td>2010</td>
<td>7221.575499</td>
<td>5</td>
<td>2010-5</td>
</tr>
<tr>
<th>4</th>
<td>2010</td>
<td>7256.515676</td>
<td>6</td>
<td>2010-6</td>
</tr>
</tbody>
</table>
</div></div></div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1">#Caso 1: inner join</span>
<span class="n">df_inner</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="n">df_ocu</span><span class="p">,</span> <span class="n">df_des</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="s2">"llave"</span><span class="p">)</span>
<span class="n">df_inner</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="n">df_inner</span><span class="p">,</span> <span class="n">df_inf</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="s2">"llave"</span><span class="p">)</span>
<span class="n">df_inner</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_html"><div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Año_x</th>
<th>ocupa</th>
<th>mes_x</th>
<th>llave</th>
<th>Año_y</th>
<th>desoc</th>
<th>mes_y</th>
<th>Año</th>
<th>Total_formal</th>
<th>Total_informal</th>
<th>Hombres_formal</th>
<th>Hombres_informal</th>
<th>Mujeres_formal</th>
<th>Mujeres_informal</th>
<th>mes</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>2017</td>
<td>8613.092511</td>
<td>8</td>
<td>2017-8</td>
<td>2017</td>
<td>647.828960</td>
<td>8</td>
<td>2017</td>
<td>6193.891633</td>
<td>2419.200879</td>
<td>3643.825292</td>
<td>1333.469417</td>
<td>2550.066341</td>
<td>1085.731462</td>
<td>8</td>
</tr>
<tr>
<th>1</th>
<td>2017</td>
<td>8622.703518</td>
<td>9</td>
<td>2017-9</td>
<td>2017</td>
<td>647.495338</td>
<td>9</td>
<td>2017</td>
<td>6168.590625</td>
<td>2454.112893</td>
<td>3639.764667</td>
<td>1354.010989</td>
<td>2528.825959</td>
<td>1100.101904</td>
<td>9</td>
</tr>
<tr>
<th>2</th>
<td>2017</td>
<td>8712.681459</td>
<td>10</td>
<td>2017-10</td>
<td>2017</td>
<td>626.061755</td>
<td>10</td>
<td>2017</td>
<td>6207.712916</td>
<td>2504.968544</td>
<td>3657.915007</td>
<td>1366.333110</td>
<td>2549.797909</td>
<td>1138.635433</td>
<td>10</td>
</tr>