La url utilizada será https://api.covid19api.com/countries
Instalamos las librerías.
!pip install pandas
Requirement already satisfied: pandas in /usr/local/lib/python3.8/dist-packages (1.3.1)
Requirement already satisfied: numpy>=1.17.3 in /usr/local/lib/python3.8/dist-packages (from pandas) (1.21.1)
Requirement already satisfied: pytz>=2017.3 in /usr/local/lib/python3.8/dist-packages (from pandas) (2020.4)
Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.8/dist-packages (from pandas) (2.8.1)
Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil>=2.7.3->pandas) (1.14.0)
Ya estaban instaladas pero así se ve el proceso. Ahora se importarían.
import pandas as pd
Definimos la url
url = 'https://api.covid19api.com/countries'
url
'https://api.covid19api.com/countries'
Creamos el dataframe.
df = pd.read_json(url)
df
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
Country | Slug | ISO2 | |
---|---|---|---|
0 | Falkland Islands (Malvinas) | falkland-islands-malvinas | FK |
1 | Panama | panama | PA |
2 | Russian Federation | russia | RU |
3 | Turkmenistan | turkmenistan | TM |
4 | Guinea | guinea | GN |
... | ... | ... | ... |
243 | Guyana | guyana | GY |
244 | Hungary | hungary | HU |
245 | Kazakhstan | kazakhstan | KZ |
246 | Liberia | liberia | LR |
247 | Somalia | somalia | SO |
248 rows × 3 columns
Así se ven solo los primeros:
df.head
<bound method NDFrame.head of Country Slug ISO2
0 Falkland Islands (Malvinas) falkland-islands-malvinas FK
1 Panama panama PA
2 Russian Federation russia RU
3 Turkmenistan turkmenistan TM
4 Guinea guinea GN
.. ... ... ...
243 Guyana guyana GY
244 Hungary hungary HU
245 Kazakhstan kazakhstan KZ
246 Liberia liberia LR
247 Somalia somalia SO
[248 rows x 3 columns]>
Así los últimos:
df.tail
<bound method NDFrame.tail of Country Slug ISO2
0 Falkland Islands (Malvinas) falkland-islands-malvinas FK
1 Panama panama PA
2 Russian Federation russia RU
3 Turkmenistan turkmenistan TM
4 Guinea guinea GN
.. ... ... ...
243 Guyana guyana GY
244 Hungary hungary HU
245 Kazakhstan kazakhstan KZ
246 Liberia liberia LR
247 Somalia somalia SO
[248 rows x 3 columns]>
Desglosamos las columnas:
df.columns
Index(['Country', 'Slug', 'ISO2'], dtype='object')
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 248 entries, 0 to 247
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Country 248 non-null object
1 Slug 248 non-null object
2 ISO2 248 non-null object
dtypes: object(3)
memory usage: 5.9+ KB
df.set_index('Country')
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
Slug | ISO2 | |
---|---|---|
Country | ||
Falkland Islands (Malvinas) | falkland-islands-malvinas | FK |
Panama | panama | PA |
Russian Federation | russia | RU |
Turkmenistan | turkmenistan | TM |
Guinea | guinea | GN |
... | ... | ... |
Guyana | guyana | GY |
Hungary | hungary | HU |
Kazakhstan | kazakhstan | KZ |
Liberia | liberia | LR |
Somalia | somalia | SO |
248 rows × 2 columns
Vamos a ver los datos de algunos países (España, Francia e Italia) en tiempo real.
url_es = 'https://api.covid19api.com/country/spain/status/confirmed/live'
url_fr = 'https://api.covid19api.com/country/france/status/confirmed/live'
url_it = 'https://api.covid19api.com/country/italy/status/confirmed/live'
df_es = pd.read_json(url_es)
df_fr = pd.read_json(url_fr)
df_it = pd.read_json(url_it)
df_es
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
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 | |||
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
883 | Spain | ES | 40.46 | -3.75 | 12613634 | confirmed | 2022-06-23 00:00:00+00:00 | |||
884 | Spain | ES | 40.46 | -3.75 | 12681820 | confirmed | 2022-06-24 00:00:00+00:00 | |||
885 | Spain | ES | 40.46 | -3.75 | 12681820 | confirmed | 2022-06-25 00:00:00+00:00 | |||
886 | Spain | ES | 40.46 | -3.75 | 12681820 | confirmed | 2022-06-26 00:00:00+00:00 | |||
887 | Spain | ES | 40.46 | -3.75 | 12681820 | confirmed | 2022-06-27 00:00:00+00:00 |
888 rows × 10 columns
df_fr
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
Country | CountryCode | Province | City | CityCode | Lat | Lon | Cases | Status | Date | |
---|---|---|---|---|---|---|---|---|---|---|
0 | France | FR | Martinique | 14.64 | -61.02 | 0 | confirmed | 2020-01-22 00:00:00+00:00 | ||
1 | France | FR | New Caledonia | -20.90 | 165.62 | 0 | confirmed | 2020-01-22 00:00:00+00:00 | ||
2 | France | FR | Reunion | -21.12 | 55.54 | 0 | confirmed | 2020-01-22 00:00:00+00:00 | ||
3 | France | FR | Wallis and Futuna | -14.29 | -178.12 | 0 | confirmed | 2020-01-22 00:00:00+00:00 | ||
4 | France | FR | French Guiana | 4.00 | -53.00 | 0 | confirmed | 2020-01-22 00:00:00+00:00 | ||
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
10652 | France | FR | Mayotte | -12.83 | 45.17 | 37877 | confirmed | 2022-06-27 00:00:00+00:00 | ||
10653 | France | FR | Saint Barthelemy | 17.90 | -62.83 | 4671 | confirmed | 2022-06-27 00:00:00+00:00 | ||
10654 | France | FR | French Guiana | 3.93 | -53.13 | 85596 | confirmed | 2022-06-27 00:00:00+00:00 | ||
10655 | France | FR | Reunion | -21.12 | 55.54 | 421269 | confirmed | 2022-06-27 00:00:00+00:00 | ||
10656 | France | FR | 46.23 | 2.21 | 29823387 | confirmed | 2022-06-28 00:00:00+00:00 |
10657 rows × 10 columns
df_it
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
Country | CountryCode | Province | City | CityCode | Lat | Lon | Cases | Status | Date | |
---|---|---|---|---|---|---|---|---|---|---|
0 | Italy | IT | 41.87 | 12.57 | 0 | confirmed | 2020-01-22 00:00:00+00:00 | |||
1 | Italy | IT | 41.87 | 12.57 | 0 | confirmed | 2020-01-23 00:00:00+00:00 | |||
2 | Italy | IT | 41.87 | 12.57 | 0 | confirmed | 2020-01-24 00:00:00+00:00 | |||
3 | Italy | IT | 41.87 | 12.57 | 0 | confirmed | 2020-01-25 00:00:00+00:00 | |||
4 | Italy | IT | 41.87 | 12.57 | 0 | confirmed | 2020-01-26 00:00:00+00:00 | |||
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
883 | Italy | IT | 41.87 | 12.57 | 18071634 | confirmed | 2022-06-23 00:00:00+00:00 | |||
884 | Italy | IT | 41.87 | 12.57 | 18128044 | confirmed | 2022-06-24 00:00:00+00:00 | |||
885 | Italy | IT | 41.87 | 12.57 | 18184917 | confirmed | 2022-06-25 00:00:00+00:00 | |||
886 | Italy | IT | 41.87 | 12.57 | 18234242 | confirmed | 2022-06-26 00:00:00+00:00 | |||
887 | Italy | IT | 41.87 | 12.57 | 18259261 | confirmed | 2022-06-27 00:00:00+00:00 |
888 rows × 10 columns
Vamos a acotarlo para ver solo los casos por fecha y se entienda mejor la tabla
df_es.set_index('Date')['Cases']
Date
2020-01-22 00:00:00+00:00 0
2020-01-23 00:00:00+00:00 0
2020-01-24 00:00:00+00:00 0
2020-01-25 00:00:00+00:00 0
2020-01-26 00:00:00+00:00 0
...
2022-06-23 00:00:00+00:00 12613634
2022-06-24 00:00:00+00:00 12681820
2022-06-25 00:00:00+00:00 12681820
2022-06-26 00:00:00+00:00 12681820
2022-06-27 00:00:00+00:00 12681820
Name: Cases, Length: 888, dtype: int64
En gráfico lo veremos mejor...
df_es.set_index('Date')['Cases'].plot(title='Casos de COVID en España')
<AxesSubplot:title={'center':'Casos de COVID en España'}, xlabel='Date'>
Según este gráfico los datos han crecido considerablemente. Podemos interpretar que a pesar de haber más casos gracias a la inmunización los soportamos mejor y no hay tantas muertes.
df_fr.set_index('Date')['Cases'].plot(title='Casos de COVID en Francia')
<AxesSubplot:title={'center':'Casos de COVID en Francia'}, xlabel='Date'>
df_it.set_index('Date')['Cases'].plot(title='Casos de COVID en Italia')
<AxesSubplot:title={'center':'Casos de COVID en Italia'}, xlabel='Date'>
Vamos a comparar la evolución de nuestros tres países en un solo gráfico.
casos_es = df_es.set_index('Date')['Cases']
casos_fr = df_fr.set_index('Date')['Cases']
casos_it = df_it.set_index('Date')['Cases']
Tras establecer estas nuevas variables, las concatenamos para después hacer el gráfico
pd.concat([casos_es,casos_fr,casos_it],axis=1)
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
Cases | Cases | Cases | |
---|---|---|---|
Date | |||
2020-01-22 00:00:00+00:00 | 0.0 | 0 | 0.0 |
2020-01-22 00:00:00+00:00 | 0.0 | 0 | 0.0 |
2020-01-22 00:00:00+00:00 | 0.0 | 0 | 0.0 |
2020-01-22 00:00:00+00:00 | 0.0 | 0 | 0.0 |
2020-01-22 00:00:00+00:00 | 0.0 | 0 | 0.0 |
... | ... | ... | ... |
2022-06-27 00:00:00+00:00 | 12681820.0 | 37877 | 18259261.0 |
2022-06-27 00:00:00+00:00 | 12681820.0 | 4671 | 18259261.0 |
2022-06-27 00:00:00+00:00 | 12681820.0 | 85596 | 18259261.0 |
2022-06-27 00:00:00+00:00 | 12681820.0 | 421269 | 18259261.0 |
2022-06-28 00:00:00+00:00 | NaN | 29823387 | NaN |
10657 rows × 3 columns
Vamos a cambiar los nombres de las columnas a los respectivos países para poder distinguirlos tanto en la tabla como en el gráfico.
vs = pd.concat([casos_es,casos_fr,casos_it],axis=1)
vs.columns = ['España','Francia','Italia']
vs
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
España | Francia | Italia | |
---|---|---|---|
Date | |||
2020-01-22 00:00:00+00:00 | 0.0 | 0 | 0.0 |
2020-01-22 00:00:00+00:00 | 0.0 | 0 | 0.0 |
2020-01-22 00:00:00+00:00 | 0.0 | 0 | 0.0 |
2020-01-22 00:00:00+00:00 | 0.0 | 0 | 0.0 |
2020-01-22 00:00:00+00:00 | 0.0 | 0 | 0.0 |
... | ... | ... | ... |
2022-06-27 00:00:00+00:00 | 12681820.0 | 37877 | 18259261.0 |
2022-06-27 00:00:00+00:00 | 12681820.0 | 4671 | 18259261.0 |
2022-06-27 00:00:00+00:00 | 12681820.0 | 85596 | 18259261.0 |
2022-06-27 00:00:00+00:00 | 12681820.0 | 421269 | 18259261.0 |
2022-06-28 00:00:00+00:00 | NaN | 29823387 | NaN |
10657 rows × 3 columns
Y finalmente hacemos plot para visualizarlo gráficamente.
vs.plot(title="Casos de COVID-19: comparativa de España, Francia e Italia")
<AxesSubplot:title={'center':'Casos de COVID-19: comparativa de España, Francia e Italia'}, xlabel='Date'>
No funciona muy bien, en las operaciones siguientes probamos con españa e italia, los datos de francia deben ser el problema. El procedimiento seguido para el gráfico de 3 es correcto.
pd.concat([casos_es,casos_it],axis=1)
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
Cases | Cases | |
---|---|---|
Date | ||
2020-01-22 00:00:00+00:00 | 0 | 0 |
2020-01-23 00:00:00+00:00 | 0 | 0 |
2020-01-24 00:00:00+00:00 | 0 | 0 |
2020-01-25 00:00:00+00:00 | 0 | 0 |
2020-01-26 00:00:00+00:00 | 0 | 0 |
... | ... | ... |
2022-06-23 00:00:00+00:00 | 12613634 | 18071634 |
2022-06-24 00:00:00+00:00 | 12681820 | 18128044 |
2022-06-25 00:00:00+00:00 | 12681820 | 18184917 |
2022-06-26 00:00:00+00:00 | 12681820 | 18234242 |
2022-06-27 00:00:00+00:00 | 12681820 | 18259261 |
888 rows × 2 columns
vs = pd.concat([casos_es,casos_it],axis=1)
vs
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
Cases | Cases | |
---|---|---|
Date | ||
2020-01-22 00:00:00+00:00 | 0.0 | 0 |
2020-01-22 00:00:00+00:00 | 0.0 | 0 |
2020-01-22 00:00:00+00:00 | 0.0 | 0 |
2020-01-22 00:00:00+00:00 | 0.0 | 0 |
2020-01-22 00:00:00+00:00 | 0.0 | 0 |
... | ... | ... |
2022-06-27 00:00:00+00:00 | 12681820.0 | 37877 |
2022-06-27 00:00:00+00:00 | 12681820.0 | 4671 |
2022-06-27 00:00:00+00:00 | 12681820.0 | 85596 |
2022-06-27 00:00:00+00:00 | 12681820.0 | 421269 |
2022-06-28 00:00:00+00:00 | NaN | 29823387 |
10657 rows × 2 columns
vs.columns = ['España','Italia']
vs.plot(title="España vs Italia")
<AxesSubplot:title={'center':'España vs Italia'}, xlabel='Date'>