Skip to content

Latest commit

 

History

History
2712 lines (2367 loc) · 47.3 KB

ad4.md

File metadata and controls

2712 lines (2367 loc) · 47.3 KB

AD4: conexión API del COVID19 y análisis con Pandas

Me conecto a la API https://api.covid19api.com/

Instalo e importo las Pandas

!pip install pandas 
Requirement already satisfied: pandas in c:\users\ania alonzo\anaconda3\lib\site-packages (1.4.2)
Requirement already satisfied: python-dateutil>=2.8.1 in c:\users\ania alonzo\anaconda3\lib\site-packages (from pandas) (2.8.2)
Requirement already satisfied: pytz>=2020.1 in c:\users\ania alonzo\anaconda3\lib\site-packages (from pandas) (2021.3)
Requirement already satisfied: numpy>=1.18.5 in c:\users\ania alonzo\anaconda3\lib\site-packages (from pandas) (1.21.5)
Requirement already satisfied: six>=1.5 in c:\users\ania alonzo\anaconda3\lib\site-packages (from python-dateutil>=2.8.1->pandas) (1.16.0)
pip install numpi
Requirement already satisfied: numpi in c:\users\ania alonzo\anaconda3\lib\site-packages (0.3.0)
Requirement already satisfied: numpy>=1.15.0 in c:\users\ania alonzo\anaconda3\lib\site-packages (from numpi) (1.21.5)
Requirement already satisfied: scipy>=1.1.0 in c:\users\ania alonzo\anaconda3\lib\site-packages (from numpi) (1.7.3)
Note: you may need to restart the kernel to use updated packages.

Coloco URL para traer la lista de países

import pandas as pd
url = 'https://api.covid19api.com/countries'
url
'https://api.covid19api.com/countries'

Añado la variable para pd.read_json

df = pd.read_json(url)
df
<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>
Country Slug ISO2
0 Gibraltar gibraltar GI
1 Oman oman OM
2 France france FR
3 Jersey jersey JE
4 Mali mali ML
... ... ... ...
243 Puerto Rico puerto-rico PR
244 Papua New Guinea papua-new-guinea PG
245 Saint Pierre and Miquelon saint-pierre-and-miquelon PM
246 Timor-Leste timor-leste TL
247 Montenegro montenegro ME

248 rows × 3 columns

Extraigo en esta ocasión sólo los datos de España y sucesivamente las de los países que me interesan seleccionar, para este trabajo lo hice con Centroamérica.

df[df['Country'] == 'Spain']
<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>
Country Slug ISO2
141 Spain spain ES
url_rt_es = 'https://api.covid19api.com/country/spain/status/confirmed/live'
df_rt_es = pd.read_json(url_rt_es)
df_rt_es
<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>
Country CountryCode Province City CityCode Lat Lon Cases Status Date
0 Spain ES 40.46 -3.75 0 confirmed 2020-01-22 00:00:00+00:00
1 Spain ES 40.46 -3.75 0 confirmed 2020-01-23 00:00:00+00:00
2 Spain ES 40.46 -3.75 0 confirmed 2020-01-24 00:00:00+00:00
3 Spain ES 40.46 -3.75 0 confirmed 2020-01-25 00:00:00+00:00
4 Spain ES 40.46 -3.75 0 confirmed 2020-01-26 00:00:00+00:00
... ... ... ... ... ... ... ... ... ... ...
894 Spain ES 40.46 -3.75 12818184 confirmed 2022-07-04 00:00:00+00:00
895 Spain ES 40.46 -3.75 12890002 confirmed 2022-07-05 00:00:00+00:00
896 Spain ES 40.46 -3.75 12890002 confirmed 2022-07-06 00:00:00+00:00
897 Spain ES 40.46 -3.75 12890002 confirmed 2022-07-07 00:00:00+00:00
898 Spain ES 40.46 -3.75 12973615 confirmed 2022-07-08 00:00:00+00:00

899 rows × 10 columns

df_rt_es.head()
<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>
Country CountryCode Province City CityCode Lat Lon Cases Status Date
0 Spain ES 40.46 -3.75 0 confirmed 2020-01-22 00:00:00+00:00
1 Spain ES 40.46 -3.75 0 confirmed 2020-01-23 00:00:00+00:00
2 Spain ES 40.46 -3.75 0 confirmed 2020-01-24 00:00:00+00:00
3 Spain ES 40.46 -3.75 0 confirmed 2020-01-25 00:00:00+00:00
4 Spain ES 40.46 -3.75 0 confirmed 2020-01-26 00:00:00+00:00
df_rt_es.tail()
<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>
Country CountryCode Province City CityCode Lat Lon Cases Status Date
894 Spain ES 40.46 -3.75 12818184 confirmed 2022-07-04 00:00:00+00:00
895 Spain ES 40.46 -3.75 12890002 confirmed 2022-07-05 00:00:00+00:00
896 Spain ES 40.46 -3.75 12890002 confirmed 2022-07-06 00:00:00+00:00
897 Spain ES 40.46 -3.75 12890002 confirmed 2022-07-07 00:00:00+00:00
898 Spain ES 40.46 -3.75 12973615 confirmed 2022-07-08 00:00:00+00:00
casos_es = df_rt_es.set_index('Date')['Cases']
casos_es.plot(title='Casos de Covid19 en España 20/1/2020 hasta 29/6/2022')
<AxesSubplot:title={'center':'Casos de Covid19 en España 20/1/2020 hasta 29/6/2022'}, xlabel='Date'>

png

df[df['Country'] == 'Costa Rica']
<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>
Country Slug ISO2
242 Costa Rica costa-rica CR
url_rt_cr = 'https://api.covid19api.com/country/costa-rica/status/confirmed/live'
df_rt_cr = pd.read_json(url_rt_cr)
df_rt_cr
<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>
Country CountryCode Province City CityCode Lat Lon Cases Status Date
0 Costa Rica CR 9.75 -83.75 0 confirmed 2020-01-22 00:00:00+00:00
1 Costa Rica CR 9.75 -83.75 0 confirmed 2020-01-23 00:00:00+00:00
2 Costa Rica CR 9.75 -83.75 0 confirmed 2020-01-24 00:00:00+00:00
3 Costa Rica CR 9.75 -83.75 0 confirmed 2020-01-25 00:00:00+00:00
4 Costa Rica CR 9.75 -83.75 0 confirmed 2020-01-26 00:00:00+00:00
... ... ... ... ... ... ... ... ... ... ...
895 Costa Rica CR 9.75 -83.75 904934 confirmed 2022-07-05 00:00:00+00:00
896 Costa Rica CR 9.75 -83.75 904934 confirmed 2022-07-06 00:00:00+00:00
897 Costa Rica CR 9.75 -83.75 904934 confirmed 2022-07-07 00:00:00+00:00
898 Costa Rica CR 9.75 -83.75 904934 confirmed 2022-07-08 00:00:00+00:00
899 Costa Rica CR 9.75 -83.75 904934 confirmed 2022-07-09 00:00:00+00:00

900 rows × 10 columns

df_rt_cr.head()
<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>
Country CountryCode Province City CityCode Lat Lon Cases Status Date
0 Costa Rica CR 9.75 -83.75 0 confirmed 2020-01-22 00:00:00+00:00
1 Costa Rica CR 9.75 -83.75 0 confirmed 2020-01-23 00:00:00+00:00
2 Costa Rica CR 9.75 -83.75 0 confirmed 2020-01-24 00:00:00+00:00
3 Costa Rica CR 9.75 -83.75 0 confirmed 2020-01-25 00:00:00+00:00
4 Costa Rica CR 9.75 -83.75 0 confirmed 2020-01-26 00:00:00+00:00
df_rt_cr.tail()
<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>
Country CountryCode Province City CityCode Lat Lon Cases Status Date
895 Costa Rica CR 9.75 -83.75 904934 confirmed 2022-07-05 00:00:00+00:00
896 Costa Rica CR 9.75 -83.75 904934 confirmed 2022-07-06 00:00:00+00:00
897 Costa Rica CR 9.75 -83.75 904934 confirmed 2022-07-07 00:00:00+00:00
898 Costa Rica CR 9.75 -83.75 904934 confirmed 2022-07-08 00:00:00+00:00
899 Costa Rica CR 9.75 -83.75 904934 confirmed 2022-07-09 00:00:00+00:00
casos_cr = df_rt_cr.set_index('Date')['Cases']
casos_cr.plot(title='Casos de Covid19 en Costa Rica 20/1/2020 hasta 29/6/2022')
<AxesSubplot:title={'center':'Casos de Covid19 en Costa Rica 20/1/2020 hasta 29/6/2022'}, xlabel='Date'>

png

df[df['Country'] == 'Honduras']
<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>
Country Slug ISO2
91 Honduras honduras HN
url_rt_hnd = 'https://api.covid19api.com/country/honduras/status/confirmed/live'
df_rt_hnd = pd.read_json(url_rt_hnd)
df_rt_hnd
<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>
Country CountryCode Province City CityCode Lat Lon Cases Status Date
0 Honduras HN 15.2 -86.24 0 confirmed 2020-01-22 00:00:00+00:00
1 Honduras HN 15.2 -86.24 0 confirmed 2020-01-23 00:00:00+00:00
2 Honduras HN 15.2 -86.24 0 confirmed 2020-01-24 00:00:00+00:00
3 Honduras HN 15.2 -86.24 0 confirmed 2020-01-25 00:00:00+00:00
4 Honduras HN 15.2 -86.24 0 confirmed 2020-01-26 00:00:00+00:00
... ... ... ... ... ... ... ... ... ... ...
895 Honduras HN 15.2 -86.24 427718 confirmed 2022-07-05 00:00:00+00:00
896 Honduras HN 15.2 -86.24 427718 confirmed 2022-07-06 00:00:00+00:00
897 Honduras HN 15.2 -86.24 427718 confirmed 2022-07-07 00:00:00+00:00
898 Honduras HN 15.2 -86.24 429408 confirmed 2022-07-08 00:00:00+00:00
899 Honduras HN 15.2 -86.24 429408 confirmed 2022-07-09 00:00:00+00:00

900 rows × 10 columns

casos_hn = df_rt_hnd.set_index('Date')['Cases']
casos_hn.plot(title='Casos de Covid19 en Honduras 20/1/2020 hasta 29/6/2022')
<AxesSubplot:title={'center':'Casos de Covid19 en Honduras 20/1/2020 hasta 29/6/2022'}, xlabel='Date'>

png

df[df['Country'] == 'Guatemala']
<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>
Country Slug ISO2
239 Guatemala guatemala GT
url_rt_guat = 'https://api.covid19api.com/country/guatemala/status/confirmed/live'
df_rt_guat = pd.read_json(url_rt_guat)
df_rt_guat
<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>
Country CountryCode Province City CityCode Lat Lon Cases Status Date
0 Guatemala GT 15.78 -90.23 0 confirmed 2020-01-22 00:00:00+00:00
1 Guatemala GT 15.78 -90.23 0 confirmed 2020-01-23 00:00:00+00:00
2 Guatemala GT 15.78 -90.23 0 confirmed 2020-01-24 00:00:00+00:00
3 Guatemala GT 15.78 -90.23 0 confirmed 2020-01-25 00:00:00+00:00
4 Guatemala GT 15.78 -90.23 0 confirmed 2020-01-26 00:00:00+00:00
... ... ... ... ... ... ... ... ... ... ...
895 Guatemala GT 15.78 -90.23 922340 confirmed 2022-07-05 00:00:00+00:00
896 Guatemala GT 15.78 -90.23 927473 confirmed 2022-07-06 00:00:00+00:00
897 Guatemala GT 15.78 -90.23 933259 confirmed 2022-07-07 00:00:00+00:00
898 Guatemala GT 15.78 -90.23 939300 confirmed 2022-07-08 00:00:00+00:00
899 Guatemala GT 15.78 -90.23 939300 confirmed 2022-07-09 00:00:00+00:00

900 rows × 10 columns

casos_guat = df_rt_guat.set_index('Date')['Cases']
casos_guat.plot(title='Casos de Covid19 en Guatemala 20/1/2020 hasta 29/6/2022')
<AxesSubplot:title={'center':'Casos de Covid19 en Guatemala 20/1/2020 hasta 29/6/2022'}, xlabel='Date'>

png

df[df['Country'] == 'El Salvador']
<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>
Country Slug ISO2
139 El Salvador el-salvador SV
url_rt_elsalv = 'https://api.covid19api.com/country/el-salvador/status/confirmed/live'
df_rt_elsalv = pd.read_json(url_rt_elsalv)
df_rt_elsalv
<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>
Country CountryCode Province City CityCode Lat Lon Cases Status Date
0 El Salvador SV 13.79 -88.9 0 confirmed 2020-01-22 00:00:00+00:00
1 El Salvador SV 13.79 -88.9 0 confirmed 2020-01-23 00:00:00+00:00
2 El Salvador SV 13.79 -88.9 0 confirmed 2020-01-24 00:00:00+00:00
3 El Salvador SV 13.79 -88.9 0 confirmed 2020-01-25 00:00:00+00:00
4 El Salvador SV 13.79 -88.9 0 confirmed 2020-01-26 00:00:00+00:00
... ... ... ... ... ... ... ... ... ... ...
895 El Salvador SV 13.79 -88.9 169646 confirmed 2022-07-05 00:00:00+00:00
896 El Salvador SV 13.79 -88.9 169646 confirmed 2022-07-06 00:00:00+00:00
897 El Salvador SV 13.79 -88.9 169646 confirmed 2022-07-07 00:00:00+00:00
898 El Salvador SV 13.79 -88.9 180970 confirmed 2022-07-08 00:00:00+00:00
899 El Salvador SV 13.79 -88.9 169646 confirmed 2022-07-09 00:00:00+00:00

900 rows × 10 columns

casos_elsalv = df_rt_elsalv.set_index('Date')['Cases']
casos_elsalv.plot(title='Casos de Covid19 en El Salvador 20/1/2020 hasta 29/6/2022')
<AxesSubplot:title={'center':'Casos de Covid19 en El Salvador 20/1/2020 hasta 29/6/2022'}, xlabel='Date'>

png

df[df['Country'] == 'Nicaragua']
<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>
Country Slug ISO2
36 Nicaragua nicaragua NI
url_rt_ni = 'https://api.covid19api.com/country/nicaragua/status/confirmed/live'
df_rt_ni = pd.read_json(url_rt_ni)
df_rt_ni
<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>
Country CountryCode Province City CityCode Lat Lon Cases Status Date
0 Nicaragua NI 12.87 -85.21 0 confirmed 2020-01-22 00:00:00+00:00
1 Nicaragua NI 12.87 -85.21 0 confirmed 2020-01-23 00:00:00+00:00
2 Nicaragua NI 12.87 -85.21 0 confirmed 2020-01-24 00:00:00+00:00
3 Nicaragua NI 12.87 -85.21 0 confirmed 2020-01-25 00:00:00+00:00
4 Nicaragua NI 12.87 -85.21 0 confirmed 2020-01-26 00:00:00+00:00
... ... ... ... ... ... ... ... ... ... ...
895 Nicaragua NI 12.87 -85.21 14690 confirmed 2022-07-05 00:00:00+00:00
896 Nicaragua NI 12.87 -85.21 14721 confirmed 2022-07-06 00:00:00+00:00
897 Nicaragua NI 12.87 -85.21 14721 confirmed 2022-07-07 00:00:00+00:00
898 Nicaragua NI 12.87 -85.21 14721 confirmed 2022-07-08 00:00:00+00:00
899 Nicaragua NI 12.87 -85.21 14721 confirmed 2022-07-09 00:00:00+00:00

900 rows × 10 columns

casos_ni = df_rt_ni.set_index('Date')['Cases']
casos_ni.plot(title='Casos de Covid19 en Nicaragua 20/1/2020 hasta 29/6/2022')
<AxesSubplot:title={'center':'Casos de Covid19 en Nicaragua 20/1/2020 hasta 29/6/2022'}, xlabel='Date'>

png

df[df['Country'] == 'Panama']
<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>
Country Slug ISO2
190 Panama panama PA
url_rt_pa = 'https://api.covid19api.com/country/panama/status/confirmed/live'
df_rt_pa = pd.read_json(url_rt_pa)
df_rt_pa
<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>
Country CountryCode Province City CityCode Lat Lon Cases Status Date
0 Panama PA 8.54 -80.78 0 confirmed 2020-01-22 00:00:00+00:00
1 Panama PA 8.54 -80.78 0 confirmed 2020-01-23 00:00:00+00:00
2 Panama PA 8.54 -80.78 0 confirmed 2020-01-24 00:00:00+00:00
3 Panama PA 8.54 -80.78 0 confirmed 2020-01-25 00:00:00+00:00
4 Panama PA 8.54 -80.78 0 confirmed 2020-01-26 00:00:00+00:00
... ... ... ... ... ... ... ... ... ... ...
895 Panama PA 8.54 -80.78 925254 confirmed 2022-07-05 00:00:00+00:00
896 Panama PA 8.54 -80.78 925254 confirmed 2022-07-06 00:00:00+00:00
897 Panama PA 8.54 -80.78 925254 confirmed 2022-07-07 00:00:00+00:00
898 Panama PA 8.54 -80.78 932710 confirmed 2022-07-08 00:00:00+00:00
899 Panama PA 8.54 -80.78 925254 confirmed 2022-07-09 00:00:00+00:00

900 rows × 10 columns

casos_pa = df_rt_pa.set_index('Date')['Cases']
casos_pa.plot(title='Casos de Covid19 en Panama 20/1/2020 hasta 29/6/2022')
<AxesSubplot:title={'center':'Casos de Covid19 en Panama 20/1/2020 hasta 29/6/2022'}, xlabel='Date'>

png

Exporto tabla por cada país centroamericano en CSV para la web

df_ca = pd.concat([casos_cr,casos_hn,casos_guat,casos_elsalv,casos_ni,casos_pa],axis=1)
df_ca
<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>
Cases Cases Cases Cases Cases Cases
Date
2020-01-22 00:00:00+00:00 0 0 0 0 0 0
2020-01-23 00:00:00+00:00 0 0 0 0 0 0
2020-01-24 00:00:00+00:00 0 0 0 0 0 0
2020-01-25 00:00:00+00:00 0 0 0 0 0 0
2020-01-26 00:00:00+00:00 0 0 0 0 0 0
... ... ... ... ... ... ...
2022-07-05 00:00:00+00:00 904934 427718 922340 169646 14690 925254
2022-07-06 00:00:00+00:00 904934 427718 927473 169646 14721 925254
2022-07-07 00:00:00+00:00 904934 427718 933259 169646 14721 925254
2022-07-08 00:00:00+00:00 904934 429408 939300 180970 14721 932710
2022-07-09 00:00:00+00:00 904934 429408 939300 169646 14721 925254

900 rows × 6 columns

df_ca.columns = ['Panama','Costa Rica','Honduras','Nicaragua','El Salvador','Guatemala']
df_ca
<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>
Panama Costa Rica Honduras Nicaragua El Salvador Guatemala
Date
2020-01-22 00:00:00+00:00 0 0 0 0 0 0
2020-01-23 00:00:00+00:00 0 0 0 0 0 0
2020-01-24 00:00:00+00:00 0 0 0 0 0 0
2020-01-25 00:00:00+00:00 0 0 0 0 0 0
2020-01-26 00:00:00+00:00 0 0 0 0 0 0
... ... ... ... ... ... ...
2022-07-05 00:00:00+00:00 904934 427718 922340 169646 14690 925254
2022-07-06 00:00:00+00:00 904934 427718 927473 169646 14721 925254
2022-07-07 00:00:00+00:00 904934 427718 933259 169646 14721 925254
2022-07-08 00:00:00+00:00 904934 429408 939300 180970 14721 932710
2022-07-09 00:00:00+00:00 904934 429408 939300 169646 14721 925254

900 rows × 6 columns

Extraigo la tabla comparativa por países de los que seleccioné, Panamá, Honduras, Costa Rica, Nicaragua, El Salvador,Guatemala

df_ca.plot(title='Comparativa Covid-19 de países Centreoamericanos', logy=True)
<AxesSubplot:title={'center':'Comparativa Covid-19 de países Centreoamericanos'}, xlabel='Date'>

png

Descripción: hice una comparación de casos de Covid-19 en Centroamérica

Todos los puntos están detallados arriba