<jupyter_start><jupyter_text>Analysis of COVID-19 in UK, GER, FR, IT, GR and BG using API. Compare the cases and deaths across time and cumulative cases on the last date of the study.<jupyter_code>import requests
import json
import pandas as pd
import scipy
import datetime as dt
import matplotlib.pyplot as plt
import seaborn as sns
import altair as alt

import numpy as np
from pandas.plotting import register_matplotlib_converters 
register_matplotlib_converters()  
from datetime import datetime,timedelta
from sklearn.metrics import mean_squared_error
from scipy.optimize import curve_fit
from scipy.optimize import fsolve
# make a request of COVID-19 API data for all countries and covert JSON format to pandas dataframe 
payload = {'code': 'ALL'} #  If you want to query just Greece, replace this line with {'code': 'Greece'}
URL = 'https://api.statworx.com/covid'
data = requests.post(url=URL, data=json.dumps(payload))
df = pd.DataFrame.from_dict(json.loads(data.text))
print(len(df))
df.head()
# convert date column (str) to datetime
df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d') 
df = df[df['date'] >= pd.to_datetime('2020-03-01') ] 
print(len(df))
df.head()
df = df.drop(['day', 'month', 'year'], axis=1)
df.head()
df.dtypes 
# select 6 countries and reset the index of this dataframe 
df = (df.loc[df['country'].isin(['United_Kingdom', 'Germany', 'France', 'Italy','Greece', 'Bulgaria'])]
      .reset_index()
      .drop(['index'], axis=1)
     )
df.head()
df.isnull().values.any()
# convert population in millions (for visualization only)
df['population_millions']=df['population']/10000000
plot_pop=sns.catplot(x='country', y='population_millions', data= df,kind="bar", height=7)
plot_pop.set_axis_labels("Country", "Population (in millions)")  
df.describe()
df.groupby('country').mean()
# define a function to plot number of cases and deaths (and their comulatives) over time for each country
def country_plot(y):
    my_tooltips = ['country', 'date', 'cases_cum', 'deaths_cum', 'deaths', 'cases']
    return alt.Chart(df).mark_line(point=True).encode(
        x='date', y = y, color='country',
        tooltip = my_tooltips
    ).properties(
        width=1000,
        height=300
    )

country_plot('cases')
# Looks like there is a data quality issue. Why in the UK at 21-May-2020 there are -525 (negative) number of cases?
country_plot('deaths')
country_plot('cases_cum')
country_plot('deaths_cum')
df.groupby('country').max()
df_last_date = df.groupby('country').max()
df_last_date
# percetage of cumulative cases and cumulative deaths from the population
df_last_date.loc[:,'cases_cum_percentage'] = df_last_date['cases_cum']/df_last_date['population'] * 100
df_last_date.loc[:,'deaths_cum_percentage'] = df_last_date['deaths_cum']/df_last_date['population'] * 100
# percetage of cumulative deaths from the cumulative cases
df_last_date.loc[:,'deaths_cum_percentage_cases'] = df_last_date['deaths_cum']/df_last_date['cases_cum'] * 100
df_last_date
my_tooltips = ['country','cases_cum_percentage', 'deaths_cum_percentage', 'deaths_cum_percentage_cases' ]

alt.Chart(df_last_date.reset_index()).transform_fold(
    fold=['cases_cum_percentage','deaths_cum_percentage']).mark_bar().encode(
    y='country:N',
    x='value:Q',
    color='key:N',
    tooltip = my_tooltips).properties(
        width=1000,
        height=300
    )
alt.Chart(df_last_date.reset_index()).mark_bar().encode(
    y='country:N',
    x='deaths_cum_percentage_cases:Q',
    tooltip = my_tooltips).properties(
        width=1000,
        height=300
    )<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13700918964076858, 'fraction_numerical': 0.017543859649122806, 'mean_word_length': 5.414285714285715, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '495617', 'license': 'MIT', 'n_tokens_mistral': 1300, 'n_tokens_neox': 1191, 'n_words': 299, 'path': 'COVID19.ipynb', 'repo_name': 'riakotti/COVID19-analysis', 'size': '3591'}