Skip to content

Files

Latest commit

 

History

History
2371 lines (2072 loc) · 41.5 KB

ad4.md

File metadata and controls

2371 lines (2072 loc) · 41.5 KB

ad4: conexión con la API del COVID-19 y análisis con Pandas

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

!pip install pandas 
Requirement already satisfied: pandas in c:\users\hp\anaconda3\lib\site-packages (1.4.2)
Requirement already satisfied: numpy>=1.18.5 in c:\users\hp\anaconda3\lib\site-packages (from pandas) (1.21.5)
Requirement already satisfied: pytz>=2020.1 in c:\users\hp\anaconda3\lib\site-packages (from pandas) (2021.3)
Requirement already satisfied: python-dateutil>=2.8.1 in c:\users\hp\anaconda3\lib\site-packages (from pandas) (2.8.2)
Requirement already satisfied: six>=1.5 in c:\users\hp\anaconda3\lib\site-packages (from python-dateutil>=2.8.1->pandas) (1.16.0)
import pandas as pd

Se tiene que instalar e importar panda

url = 'https://api.covid19api.com/countries'

Se coloca la url para llamar a la lista de países

url
'https://api.covid19api.com/countries'
df = pd.read_json(url)

Añado una variable para la función read_json

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

El código permite extraer la lista parte de las filas

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

Aplicando la url y la funciòn read_json llamo a una lista de casos de España

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
df.describe()
<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
count 248 248 248
unique 248 248 248
top Gibraltar gibraltar GI
freq 1 1 1
plot_rt_es = df_rt_es.set_index('Date')['Cases'].plot(title="Casos de Covid-19 en España desde 20/01/2020 hasta 29/06/2022")

png

Profesor pide repetir el proceso con Panamá

df[df['Country'] == 'Pan']
<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
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 Covid-19 en Panama desde 20/01/2020 hasta 29/06/2022")
<AxesSubplot:title={'center':'Casos de Covid-19 en Panama desde 20/01/2020 hasta 29/06/2022'}, xlabel='Date'>

png

Costa Rica

Repito el mismo proceso que he hecho con Panamá, con Costa Rica, Honduras, Nicaragua, Guatemala y El Salvador

df[df['Country'] == '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 Slug ISO2
url_casos_cr = 'https://api.covid19api.com/country/cr/status/confirmed/live'
df_rt_cr = pd.read_json(url_casos_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

casos_cr = df_rt_cr.set_index('Date')['Cases']
casos_cr.plot(title="Casos de Covid-19 en Costa Rica desde 20/01/2020 hasta 29/06/2022")
<AxesSubplot:title={'center':'Casos de Covid-19 en Costa Rica desde 20/01/2020 hasta 29/06/2022'}, xlabel='Date'>

png

Honduras

url_casos_hnd = 'https://api.covid19api.com/country/hnd/status/confirmed/live'
df_rt_hnd = pd.read_json(url_casos_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

Gráfico

casos_hnd = df_rt_hnd.set_index('Date')['Cases']
casos_hnd.plot(title="Casos de Covid-19 en Honduras desde 20/01/2020 hasta 29/06/2022")
<AxesSubplot:title={'center':'Casos de Covid-19 en Honduras desde 20/01/2020 hasta 29/06/2022'}, xlabel='Date'>

png

Guatemala

url_casos_guat = 'https://api.covid19api.com/country/guatemala/status/confirmed/live'
df_rt_guat = pd.read_json(url_casos_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 Covid-19 en Guatemala desde 20/01/2020 hasta 29/06/2022")
<AxesSubplot:title={'center':'Casos de Covid-19 en Guatemala desde 20/01/2020 hasta 29/06/2022'}, xlabel='Date'>

png

El Salvador

url_casos_elsalv = 'https://api.covid19api.com/country/el-salvador/status/confirmed/live'
df_rt_elsalv = pd.read_json(url_casos_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 Covid-19 en el-salavdor desde 20/01/2020 hasta 29/06/2022")
<AxesSubplot:title={'center':'Casos de Covid-19 en el-salavdor desde 20/01/2020 hasta 29/06/2022'}, xlabel='Date'>

png

Nicaragua

url_casos_ni = 'https://api.Covid19api.com/country/nicaragua/status/confirmed/live'
df_rt_ni = pd.read_json(url_casos_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 Covid-19 en Nicaragua desde 20/01/2020 hasta 29/06/2022")
<AxesSubplot:title={'center':'Casos de Covid-19 en Nicaragua desde 20/01/2020 hasta 29/06/2022'}, xlabel='Date'>

png

Plotamos países

df_ca = pd.concat([casos_pa,casos_cr,casos_hnd,casos_elsalv,casos_guat,casos_ni],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 925254 904934 904934 169646 922340 922340
2022-07-06 00:00:00+00:00 925254 904934 904934 169646 927473 927473
2022-07-07 00:00:00+00:00 925254 904934 904934 169646 933259 933259
2022-07-08 00:00:00+00:00 932710 904934 904934 180970 939300 939300
2022-07-09 00:00:00+00:00 925254 904934 904934 169646 939300 939300

900 rows × 6 columns

df_ca.columns = ['Panamá','Costa Rica','Honduras','Guatemala','El Salvador','Nicaragua']
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>
Panamá Costa Rica Honduras Guatemala El Salvador Nicaragua
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 925254 904934 904934 169646 922340 922340
2022-07-06 00:00:00+00:00 925254 904934 904934 169646 927473 927473
2022-07-07 00:00:00+00:00 925254 904934 904934 169646 933259 933259
2022-07-08 00:00:00+00:00 932710 904934 904934 180970 939300 939300
2022-07-09 00:00:00+00:00 925254 904934 904934 169646 939300 939300

900 rows × 6 columns

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

png

Descripción: realicé una comparación de todos los países de Centroamérica.

  • Para lograrlo, lo primero que hice fue extraer los casos por paìs y posteriormente realicé la comparación de todas las regiones.