# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="XBtZdDTczUAL"
# # Introduction
#
# Theme 2: Healthcare
# https://www.kaggle.com/gpreda/covid-world-vaccination-progress
# * Q1: Which country has the highest & lowest % of population vaccinated?
# * Q2: Is there any correlation between the number of population vaccinated vs international borders relaxing? 
#
# Next week, you will present your solution + reasons in a 3-slide presentation to any one of the themes you have chosen above. Maximum presentation time is 5 minutes, tops!

# + [markdown] id="1JRA-Lc_1SPP"
# # Initialize

# + colab={"base_uri": "https://localhost:8080/"} id="nxmMJO8H1UUv" executionInfo={"status": "ok", "timestamp": 1624738682550, "user_tz": -60, "elapsed": 17600, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}} outputId="ccf02cbd-ba87-457c-a3a8-3c3b12bf3f0e"
# Mount the drive folder
from google.colab import drive # to load data from google drive
drive.mount("/content/drive")

# + id="IX-4yBcM1XPb" executionInfo={"status": "ok", "timestamp": 1624738685443, "user_tz": -60, "elapsed": 1101, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}}
# Load libraries
import os # For files operations
import urllib.request # For download from url
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt # ploting the data
import seaborn as sns # ploting the data
import csv # to import data in txt files

# + id="PKIW_bdB1aJY" executionInfo={"status": "ok", "timestamp": 1624738688054, "user_tz": -60, "elapsed": 180, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}}
# Set up color blind friendly color palette
# The palette with grey:
cbPalette = ["#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7"]
# The palette with black:
cbbPalette = ["#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7"]

# sns.palplot(sns.color_palette(cbPalette))
# sns.palplot(sns.color_palette(cbbPalette))

sns.set_palette(cbPalette)
#sns.set_palette(cbbPalette)

# + id="l-u6YoBc1cyJ" executionInfo={"status": "ok", "timestamp": 1624738690223, "user_tz": -60, "elapsed": 210, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}}
# Seaborn favourite plot shortcuts
def boxplot(data, x, y, title = ""):
    
    """
    This function generates a seaborn boxplot with my defaults parameters.

    Parameters:
        title (string) title of the plot, default is empty
        data (df) the data frame
        x (panda serie) the x axis
        y (panda serie) the y axis
    """

    f, ax = plt.subplots(figsize=(8, 6))
    sns.boxplot(x=x, y=y, data=data, notch=True, showmeans=True,
           meanprops={"marker":"s","markerfacecolor":"white", "markeredgecolor":"black"})
    plt.title(title)
    plt.ioff()

def countplot(data, variable, title = ""):
    """
    This function contains my favourite parameters for the seaborn coutplot plot
    """

    f, ax = plt.subplots(figsize=(8, 6))
    sns.countplot(data=data, x=variable)
    plt.title(title)
    plt.ioff()


# + colab={"base_uri": "https://localhost:8080/"} id="BJUxHodQ3soU" executionInfo={"status": "ok", "timestamp": 1624738692874, "user_tz": -60, "elapsed": 557, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}} outputId="36c2fac4-c077-4230-ba22-aba9eaf044d5"
# Set up the path for the data and output folders to the challenge and list files
PATH = "/content/drive/MyDrive/Data_science/DSAK"
data_dir = PATH + "/Data"
output_dir = PATH + "/Output"
os.listdir(data_dir)

# + [markdown] id="ZcxYvbEj0TVr"
# # Q1: Which country has the highest and lowest percentage of population vaccinated?

# + [markdown] id="uPcr26ez5s3N"
# ## Using the manufacturer dataset

# + colab={"base_uri": "https://localhost:8080/"} id="hRkzIhH-32az" executionInfo={"status": "ok", "timestamp": 1624738696234, "user_tz": -60, "elapsed": 474, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}} outputId="73cea3ea-0890-41d4-d35e-226dcb23df08"
file = data_dir + "/" + "country_vaccinations_by_manufacturer.csv"
vaccine_manufacturer = pd.read_csv(file, sep = ',', encoding = 'UTF-8')
vaccine_manufacturer .info()

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="ECpoD_Nq6NLj" executionInfo={"status": "ok", "timestamp": 1624738698024, "user_tz": -60, "elapsed": 304, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}} outputId="ad89d29c-8b44-4abe-f0ac-22b2e2300e0e"
vaccine_manufacturer.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 235} id="AUR9ZGBE6SEg" executionInfo={"status": "ok", "timestamp": 1624738700508, "user_tz": -60, "elapsed": 232, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}} outputId="339c7407-ed85-4236-bbe0-6f24be1eefce"
# Compute the total of vaccine for each country by location
vaccine_manufacturer .rename(columns={'location': 'Country'}, inplace=True)
df = vaccine_manufacturer .groupby(['Country'])[['total_vaccinations']].max()
df.head()

# + colab={"base_uri": "https://localhost:8080/"} id="j924Bvr46am7" executionInfo={"status": "ok", "timestamp": 1624738702674, "user_tz": -60, "elapsed": 514, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}} outputId="5a15c9a3-7936-4c79-c255-e4cc4be0f092"
# Combine with the world population dataset

file = data_dir + "/" + "population_by_country_2020.csv"
population = pd.read_csv(file, sep = ',', encoding = 'UTF-8')
population.info()

# + colab={"base_uri": "https://localhost:8080/", "height": 221} id="ouTX5gpw6eCa" executionInfo={"status": "ok", "timestamp": 1624738705153, "user_tz": -60, "elapsed": 277, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}} outputId="f4fcfcbf-00a3-4585-b97d-3027f380b6fa"
population.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="UXSHvHW3BcKn" executionInfo={"status": "ok", "timestamp": 1624738708162, "user_tz": -60, "elapsed": 217, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}} outputId="950a103c-e4b3-4a36-aa52-4e759cda6e19"
df2 = population[['Country (or dependency)', 'Population (2020)', 'Med. Age', 'Urban Pop %', 'World Share']].copy()

df2.rename(columns={'Country (or dependency)': 'Country'}, inplace=True)
df2.rename(columns={'Population (2020)': 'Population'}, inplace=True)
df2.rename(columns={'Med. Age': 'Median_age'}, inplace=True)
df2.rename(columns={'Urban Pop %': 'Urban_pop_percentage'}, inplace=True)

df2.head()

# + colab={"base_uri": "https://localhost:8080/"} id="fwCAacn-b63h" executionInfo={"status": "ok", "timestamp": 1624738710896, "user_tz": -60, "elapsed": 315, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}} outputId="9b210f9a-e6d2-46ba-c7f6-ed9f14f97a35"
vaccine_manufacturer.Country.unique()

# + colab={"base_uri": "https://localhost:8080/"} id="1u75S-wkjp4F" executionInfo={"status": "ok", "timestamp": 1624738713060, "user_tz": -60, "elapsed": 199, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}} outputId="73111af6-1731-4a53-d387-37a8309811d1"
vaccine_manufacturer = vaccine_manufacturer.groupby(["Country"])["total_vaccinations"].max()
vaccine_manufacturer.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="hPEt53HDE2u3" executionInfo={"status": "ok", "timestamp": 1624738715109, "user_tz": -60, "elapsed": 236, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}} outputId="1f4aef39-beda-44d5-d6aa-9abde601f76e"
# Combine the datasets
# The UK was lost in the process. I would beed to check that the countries names
# corresponds in both datasets

merged = pd.merge(vaccine_manufacturer, df2, on = "Country")
merged.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="09rQDJ9aTZM5" executionInfo={"status": "ok", "timestamp": 1624738717637, "user_tz": -60, "elapsed": 186, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}} outputId="02fde68c-9cf1-4821-9248-58c26808a51c"
# Add a new column percentage of population vaccinated
# percentage_vaccinated = total_vaccinations/Population * 100

merged["percentage_vaccinated"] = merged["total_vaccinations"] / merged["Population"] * 100
merged.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 622} id="Bp3rfp-eqoXG" executionInfo={"status": "ok", "timestamp": 1624738720646, "user_tz": -60, "elapsed": 770, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}} outputId="fd55923b-8776-421a-8dde-24fcb383aa36"
# Plot percentage of vaccinated people per country
# https://datavizpyr.com/sort-bars-in-barplot-using-seaborn-in-python/

title = 'Percentage of vaccinated people per country'
data = merged.copy()
#data.sort_values(by=['percentage_vaccinated']).reset_index()
y = "Country"
x = "percentage_vaccinated"

f, ax = plt.subplots(figsize=(8, 10))
sns.barplot(x=x, y=y, data=data, color = "#E69F00",
            order=data.sort_values('percentage_vaccinated', ascending=False).Country)
plt.title(title)
plt.ioff()

# + [markdown] id="DxykcN5FygYw"
# After looking closely at the data set it appear that many countries, such as the UK, are missing from this dataset. 

# + [markdown] id="kdcCB5WJ50MX"
# ## Using the vaccine dataset

# + colab={"base_uri": "https://localhost:8080/"} id="ijxCaA5e55sd" executionInfo={"status": "ok", "timestamp": 1624740065256, "user_tz": -60, "elapsed": 630, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}} outputId="36753c16-2178-49ce-de24-fd0a05c3825d"
file = data_dir + "/" + "country_vaccinations.csv"
vaccine = pd.read_csv(file, sep = ',', encoding = 'UTF-8')
vaccine.info()

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="MnLdxaRO6eEm" executionInfo={"status": "ok", "timestamp": 1624740066683, "user_tz": -60, "elapsed": 8, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}} outputId="cb0f8ab1-2baa-4122-fd66-cfb6e7e401bd"
vaccine = vaccine[['country', 'date', 'total_vaccinations', 'people_fully_vaccinated', 'vaccines']]
vaccine.rename(columns={'country': 'Country'}, inplace=True)
vaccine.head()

# + colab={"base_uri": "https://localhost:8080/"} id="sIzIROcB_yf9" executionInfo={"status": "ok", "timestamp": 1624740069717, "user_tz": -60, "elapsed": 4, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}} outputId="ed6c32ee-4721-4c55-dfcc-2f60a281b1b0"
vaccine = vaccine.groupby(["Country"])["people_fully_vaccinated"].max()
vaccine.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="Im5F_s1F8xGw" executionInfo={"status": "ok", "timestamp": 1624740073234, "user_tz": -60, "elapsed": 925, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}} outputId="c69bca68-e967-4fb2-87f6-274628a58c98"
# Combine the datasets
# The UK was lost in the process. I would beed to check that the countries names
# corresponds in both datasets

merged2 = pd.merge(vaccine, df2, on = "Country")
merged2.head()

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="CxJm0UO59Igp" executionInfo={"status": "ok", "timestamp": 1624740365182, "user_tz": -60, "elapsed": 213, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}} outputId="834816e2-0da3-4d1a-a6ed-6947e1207d07"
# Add a new column percentage of population vaccinated
# percentage_vaccinated = total_vaccinations/Population * 100

merged2["percentage_vaccinated"] = merged2["people_fully_vaccinated"] / merged2["Population"] * 100
merged2.head()

# + id="vVxF0SHrBNRG" executionInfo={"status": "ok", "timestamp": 1624741050657, "user_tz": -60, "elapsed": 216, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}}
# filter country with higher coverage
high_coverage = merged2.loc[(merged2['percentage_vaccinated'] >= 40) & (merged2['percentage_vaccinated'] < 100)]
high_coverage = high_coverage.sort_values(by=['percentage_vaccinated'], ascending=False).reset_index()
high_coverage = high_coverage.head(n=20)

# + id="p6xNos6k9K_V" colab={"base_uri": "https://localhost:8080/", "height": 405} executionInfo={"status": "ok", "timestamp": 1624741052193, "user_tz": -60, "elapsed": 311, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}} outputId="46781ed8-22fe-4757-b540-b4aa975bb982"
# Plot percentage of vaccinated people per country
# https://datavizpyr.com/sort-bars-in-barplot-using-seaborn-in-python/

title = 'Percentage of vaccinated people per country'
data = high_coverage
#data.sort_values(by=['percentage_vaccinated']).reset_index()
y = "Country"
x = "percentage_vaccinated"

f, ax = plt.subplots(figsize=(8, 6))
sns.barplot(x=x, y=y, data=data, color = "#E69F00",
            order=data.sort_values('percentage_vaccinated', ascending=False).Country)
plt.title(title)
plt.ioff()

# + id="UxoNSw9WFilh" executionInfo={"status": "ok", "timestamp": 1624740800583, "user_tz": -60, "elapsed": 307, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}}
# filter country with lower coverage
low_coverage = merged2.loc[(merged2['percentage_vaccinated'] < 5)]
low_coverage = low_coverage.sort_values(by=['percentage_vaccinated'], ascending=True).reset_index()
low_coverage = low_coverage.head(n=20)

# + colab={"base_uri": "https://localhost:8080/", "height": 405} id="sL0jKqI8FqGb" executionInfo={"status": "ok", "timestamp": 1624741134228, "user_tz": -60, "elapsed": 228, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjK0v36XxtzMAuNrpVnOXhqBpTN8QL_OYny8mh1mw=s64", "userId": "13891238098951500126"}} outputId="8d4dfcbf-c584-4a83-d799-8f8bbf0061e7"
# Plot percentage of vaccinated people per country
# https://datavizpyr.com/sort-bars-in-barplot-using-seaborn-in-python/

title = 'Percentage of vaccinated people per country'
data = low_coverage
y = "Country"
x = "percentage_vaccinated"

f, ax = plt.subplots(figsize=(8, 6))
sns.barplot(x=x, y=y, data=data, color = "#56B4E9",
            order=data.sort_values('percentage_vaccinated', ascending=False).Country)
plt.title(title)
plt.ioff()

# + [markdown] id="nU6h3SA8vzL1"
# # Q2: Is there any correlation between the number of population vaccinated vs international borders relaxing?
#
# For this question we would need a dataset on border relaxing. This is probably hard to find. 
#
# * https://www.kayak.com/travel-restrictions
# * https://casinodata.io/
#
# The data was manaully collected form casinodata Sat 26 June 21 art 19:02.

# + id="zZWCkoAGfsq-"
# Combine with the world population dataset

file = data_dir + "/" + "Border_status.csv"
border = pd.read_csv(file, sep = ',', encoding = 'UTF-8')
border.info()

# + id="xTPnf7k_iRr7"
# Combine the datasets

df = pd.merge(merged, border, on = "Country")
df.head()

# + id="kbfTnykRib2p"
# Plot numner of vaccine by borders status

title = "Vaccination coverage per border status for tourists"
data = df
x = "Border_status_tourist"
y = "percentage_vaccinated"

f, ax = plt.subplots(figsize=(8, 6))
sns.boxplot(x=x, y=y, data=data, showmeans=True,
           meanprops={"marker":"s","markerfacecolor":"white", "markeredgecolor":"black"})
plt.title(title)
plt.ioff()

# + id="KDFQF3r1lqhC"
# Plot numner of vaccine by borders status

title = "Vaccination coverage per border status for residents"
data = df
x = "Border_status_resident"
y = "percentage_vaccinated"

f, ax = plt.subplots(figsize=(8, 6))
sns.boxplot(x=x, y=y, data=data, showmeans=True,
           meanprops={"marker":"s","markerfacecolor":"white", "markeredgecolor":"black"})
plt.title(title)
plt.ioff()

StarcoderdataJupyterScriptsDedupFiltered