hola hola hola
La url que usaremos es: https://api.covid19api.com/countries
Primero vamos a importar 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: python-dateutil>=2.7.3 in /usr/local/lib/python3.8/dist-packages (from pandas) (2.8.1)
Requirement already satisfied: pytz>=2017.3 in /usr/local/lib/python3.8/dist-packages (from pandas) (2020.4)
Requirement already satisfied: six>=1.5 in /usr/lib/python3/dist-packages (from python-dateutil>=2.7.3->pandas) (1.14.0)
Perdón, lo de antes era instalar las librerías. Ahora sí, vamos a importarlas.
import pd as pandas
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-4-d001e6b1a3c9> in <module>
----> 1 import pd as pandas
ModuleNotFoundError: No module named 'pd'
import pandas as pd
Ahora definimos la url que vamos a usar como variable.
url = 'https://api.covid19api.com/countries'
Comprobamos que se ha fijado...
url
'https://api.covid19api.com/countries'
Bien. Ahora ya podemos crear el dataframe.
En este caso, a diferencia del de las farolas, se trata de un archivo json.
df = pd.read_json(url)
df
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
</style>
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
Country | Slug | ISO2 | |
---|---|---|---|
0 | Malaysia | malaysia | MY |
1 | Maldives | maldives | MV |
2 | Mozambique | mozambique | MZ |
3 | Turks and Caicos Islands | turks-and-caicos-islands | TC |
4 | Viet Nam | vietnam | VN |
... | ... | ... | ... |
243 | Oman | oman | OM |
244 | Zimbabwe | zimbabwe | ZW |
245 | Gibraltar | gibraltar | GI |
246 | Germany | germany | DE |
247 | Marshall Islands | marshall-islands | MH |
248 rows × 3 columns
Juguemos un poco con la tabla acotándola. ¿Qué columnas tiene?
df.columns
Index(['Country', 'Slug', 'ISO2'], dtype='object')
df.head()
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
</style>
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
Country | Slug | ISO2 | |
---|---|---|---|
0 | Malaysia | malaysia | MY |
1 | Maldives | maldives | MV |
2 | Mozambique | mozambique | MZ |
3 | Turks and Caicos Islands | turks-and-caicos-islands | TC |
4 | Viet Nam | vietnam | VN |
df.tail()
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
</style>
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
Country | Slug | ISO2 | |
---|---|---|---|
243 | Oman | oman | OM |
244 | Zimbabwe | zimbabwe | ZW |
245 | Gibraltar | gibraltar | GI |
246 | Germany | germany | DE |
247 | Marshall Islands | marshall-islands | MH |
df.info
<bound method DataFrame.info of Country Slug ISO2
0 Malaysia malaysia MY
1 Maldives maldives MV
2 Mozambique mozambique MZ
3 Turks and Caicos Islands turks-and-caicos-islands TC
4 Viet Nam vietnam VN
.. ... ... ...
243 Oman oman OM
244 Zimbabwe zimbabwe ZW
245 Gibraltar gibraltar GI
246 Germany germany DE
247 Marshall Islands marshall-islands MH
[248 rows x 3 columns]>
Vamos a centrarnos en uno de los países, españa.
url_es = 'https://api.covid19api.com/country/spain/status/confirmed/live'
url_es
'https://api.covid19api.com/country/spain/status/confirmed/live'
df = pd.read_json(url_es)
df = pd.read_json(url)
df
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
</style>
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
Country | Slug | ISO2 | |
---|---|---|---|
0 | Malaysia | malaysia | MY |
1 | Maldives | maldives | MV |
2 | Mozambique | mozambique | MZ |
3 | Turks and Caicos Islands | turks-and-caicos-islands | TC |
4 | Viet Nam | vietnam | VN |
... | ... | ... | ... |
243 | Oman | oman | OM |
244 | Zimbabwe | zimbabwe | ZW |
245 | Gibraltar | gibraltar | GI |
246 | Germany | germany | DE |
247 | Marshall Islands | marshall-islands | MH |
248 rows × 3 columns
df_es = pd.read_json(url_es)
df_es
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
</style>
.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 | |||
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
882 | Spain | ES | 40.46 | -3.75 | 12613634 | confirmed | 2022-06-22 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 rows × 10 columns
url_it = 'https://api.covid19api.com/country/italy/status/confirmed/live'
url_it
'https://api.covid19api.com/country/italy/status/confirmed/live'
df_it = pd.read_json(url_it)
df_it
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
</style>
.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 | |||
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
882 | Italy | IT | 41.87 | 12.57 | 18014202 | confirmed | 2022-06-22 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 rows × 10 columns
url_gr = 'https://api.covid19api.com/country/greece/status/confirmed/live'
url_gr
'https://api.covid19api.com/country/greece/status/confirmed/live'
df_gr = pd.read_json(url_gr)
df_gr
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
</style>
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
Country | CountryCode | Province | City | CityCode | Lat | Lon | Cases | Status | Date | |
---|---|---|---|---|---|---|---|---|---|---|
0 | Greece | GR | 39.07 | 21.82 | 0 | confirmed | 2020-01-22 00:00:00+00:00 | |||
1 | Greece | GR | 39.07 | 21.82 | 0 | confirmed | 2020-01-23 00:00:00+00:00 | |||
2 | Greece | GR | 39.07 | 21.82 | 0 | confirmed | 2020-01-24 00:00:00+00:00 | |||
3 | Greece | GR | 39.07 | 21.82 | 0 | confirmed | 2020-01-25 00:00:00+00:00 | |||
4 | Greece | GR | 39.07 | 21.82 | 0 | confirmed | 2020-01-26 00:00:00+00:00 | |||
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
883 | Greece | GR | 39.07 | 21.82 | 3583526 | confirmed | 2022-06-23 00:00:00+00:00 | |||
884 | Greece | GR | 39.07 | 21.82 | 3595498 | confirmed | 2022-06-24 00:00:00+00:00 | |||
885 | Greece | GR | 39.07 | 21.82 | 3607580 | confirmed | 2022-06-25 00:00:00+00:00 | |||
886 | Greece | GR | 39.07 | 21.82 | 3616874 | confirmed | 2022-06-26 00:00:00+00:00 | |||
887 | Greece | GR | 39.07 | 21.82 | 3616874 | confirmed | 2022-06-27 00:00:00+00:00 |
888 rows × 10 columns
df_es.set_index('Date')
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
</style>
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
Country | CountryCode | Province | City | CityCode | Lat | Lon | Cases | Status | |
---|---|---|---|---|---|---|---|---|---|
Date | |||||||||
2020-01-22 00:00:00+00:00 | Spain | ES | 40.46 | -3.75 | 0 | confirmed | |||
2020-01-23 00:00:00+00:00 | Spain | ES | 40.46 | -3.75 | 0 | confirmed | |||
2020-01-24 00:00:00+00:00 | Spain | ES | 40.46 | -3.75 | 0 | confirmed | |||
2020-01-25 00:00:00+00:00 | Spain | ES | 40.46 | -3.75 | 0 | confirmed | |||
2020-01-26 00:00:00+00:00 | Spain | ES | 40.46 | -3.75 | 0 | confirmed | |||
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
2022-06-22 00:00:00+00:00 | Spain | ES | 40.46 | -3.75 | 12613634 | confirmed | |||
2022-06-23 00:00:00+00:00 | Spain | ES | 40.46 | -3.75 | 12613634 | confirmed | |||
2022-06-24 00:00:00+00:00 | Spain | ES | 40.46 | -3.75 | 12681820 | confirmed | |||
2022-06-25 00:00:00+00:00 | Spain | ES | 40.46 | -3.75 | 12681820 | confirmed | |||
2022-06-26 00:00:00+00:00 | Spain | ES | 40.46 | -3.75 | 12681820 | confirmed |
887 rows × 9 columns
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-22 00:00:00+00:00 12613634
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
Name: Cases, Length: 887, dtype: int64
df_it.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-22 00:00:00+00:00 18014202
2022-06-23 00:00:00+00:00 18071634
2022-06-24 00:00:00+00:00 18128044
2022-06-25 00:00:00+00:00 18184917
2022-06-26 00:00:00+00:00 18234242
Name: Cases, Length: 887, dtype: int64
df_gr.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 3583526
2022-06-24 00:00:00+00:00 3595498
2022-06-25 00:00:00+00:00 3607580
2022-06-26 00:00:00+00:00 3616874
2022-06-27 00:00:00+00:00 3616874
Name: Cases, Length: 888, dtype: int64
df_es.set_index('Date')['Cases'].plot(title='Casos de COVID-19 en España')
<AxesSubplot:title={'center':'Casos de COVID-19 en España'}, xlabel='Date'>
df_es.set_index('Cases')['Date'].plot(title='prueba de al revés')
<AxesSubplot:title={'center':'prueba de al revés'}, xlabel='Cases'>
b
<AxesSubplot:title={'center':'Casos de COVID-19 en Italia'}, xlabel='Date'>
df_gr.set_index('Date')['Cases'].plot(title='Casos de COVID-19 en Grecia')
<AxesSubplot:title={'center':'Casos de COVID-19 en Grecia'}, xlabel='Date'>
casos_es = df_es.set_index('Cases')['Date']
casos_it = df_it.set_index('Cases')['Date']
casos_gr = df_gr.set_index('Cases')['Date']
casos_es = df_es.set_index('Date')['Cases']
casos_es
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-22 00:00:00+00:00 12613634
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
Name: Cases, Length: 887, dtype: int64
casos_it = df_it.set_index('Date')['Cases']
casos_it
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-22 00:00:00+00:00 18014202
2022-06-23 00:00:00+00:00 18071634
2022-06-24 00:00:00+00:00 18128044
2022-06-25 00:00:00+00:00 18184917
2022-06-26 00:00:00+00:00 18234242
Name: Cases, Length: 887, dtype: int64
casos_gr = df_gr.set_index('Date')['Cases']
casos_gr
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 3583526
2022-06-24 00:00:00+00:00 3595498
2022-06-25 00:00:00+00:00 3607580
2022-06-26 00:00:00+00:00 3616874
2022-06-27 00:00:00+00:00 3616874
Name: Cases, Length: 888, dtype: int64
vs = pd.concat([casos_es,casos_it];axis=1)
File "<ipython-input-56-8995b982f51b>", line 1
vs = pd.concat([casos_es,casos_it];axis=1)
^
SyntaxError: invalid syntax
pd.concat([casos_es,casos_it],axis=1)
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
</style>
.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-22 00:00:00+00:00 | 12613634 | 18014202 |
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 |
887 rows × 2 columns
vs = pd.concat([casos_es,casos_it],axis=1)
vs
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
</style>
.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-22 00:00:00+00:00 | 12613634 | 18014202 |
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 |
887 rows × 2 columns
pd.concat([casos_es,casos_it,casos_gr],axis=1)
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
</style>
.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-23 00:00:00+00:00 | 0.0 | 0.0 | 0 |
2020-01-24 00:00:00+00:00 | 0.0 | 0.0 | 0 |
2020-01-25 00:00:00+00:00 | 0.0 | 0.0 | 0 |
2020-01-26 00:00:00+00:00 | 0.0 | 0.0 | 0 |
... | ... | ... | ... |
2022-06-23 00:00:00+00:00 | 12613634.0 | 18071634.0 | 3583526 |
2022-06-24 00:00:00+00:00 | 12681820.0 | 18128044.0 | 3595498 |
2022-06-25 00:00:00+00:00 | 12681820.0 | 18184917.0 | 3607580 |
2022-06-26 00:00:00+00:00 | 12681820.0 | 18234242.0 | 3616874 |
2022-06-27 00:00:00+00:00 | NaN | NaN | 3616874 |
888 rows × 3 columns
vs = pd.concat([casos_es,casos_it,casos_gr],axis=1)
vs
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
</style>
.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-23 00:00:00+00:00 | 0.0 | 0.0 | 0 |
2020-01-24 00:00:00+00:00 | 0.0 | 0.0 | 0 |
2020-01-25 00:00:00+00:00 | 0.0 | 0.0 | 0 |
2020-01-26 00:00:00+00:00 | 0.0 | 0.0 | 0 |
... | ... | ... | ... |
2022-06-23 00:00:00+00:00 | 12613634.0 | 18071634.0 | 3583526 |
2022-06-24 00:00:00+00:00 | 12681820.0 | 18128044.0 | 3595498 |
2022-06-25 00:00:00+00:00 | 12681820.0 | 18184917.0 | 3607580 |
2022-06-26 00:00:00+00:00 | 12681820.0 | 18234242.0 | 3616874 |
2022-06-27 00:00:00+00:00 | NaN | NaN | 3616874 |
888 rows × 3 columns
vs.columns = ['España','Italia','Grecia']
vs.columns
Index(['España', 'Italia', 'Grecia'], dtype='object')
vs.plot(title='Casos de COVID-19 en España, Italia y Grecia')
<AxesSubplot:title={'center':'Casos de COVID-19 en España, Italia y Grecia'}, xlabel='Date'>