text_html_output_1.png text_plain_output_1.png
import datetime

import os

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import plotly.express as px

import plotly.io as pio

import plotly.offline as py

import numpy as np
import pandas as pd
from plotly import tools, subplots
import plotly.offline as py
py.init_notebook_mode(connected=True)
import plotly.graph_objs as go
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.figure_factory as ff
import plotly.io as pio
pio.templates.default = 'plotly_dark'
import os
import datetime

train_df = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
test_df = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv')
submission_df = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/submission.csv')
train_df = train_df.drop(['Id'], axis=1)
train_df.rename(columns={'Country_Region': 'Country'}, inplace=True)
test_df.rename(columns={'Country_Region': 'Country'}, inplace=True)
train_df['Province_State'].fillna('None*', inplace=True)
test_df['Province_State'].fillna('None*', inplace=True)
renameCountryNames = {'Congo (Brazzaville)': 'Congo1', 'Congo (Kinshasa)': 'Congo2', "Cote d'Ivoire": "Côte d'Ivoire", 'Czechia': 'Czech Republic (Czechia)', 'Korea, South': 'South Korea', 'Saint Kitts and Nevis': 'Saint Kitts & Nevis', 'Saint Vincent and the Grenadines': 'St. Vincent & Grenadines', 'Taiwan*': 'Taiwan', 'US': 'United States'}
train_df.replace({'Country': renameCountryNames}, inplace=True)
test_df.replace({'Country': renameCountryNames}, inplace=True)
specific_countries = ['United States', 'United Kingdom', 'Netherlands']
days_df = train_df['Date'].apply(lambda dt: datetime.datetime.strptime(dt, '%Y-%m-%d') - datetime.datetime.strptime('2020-01-21', '%Y-%m-%d')).apply(lambda x: str(x).split()[0]).astype(int)
train_df['Days'] = days_df
days_df = test_df['Date'].apply(lambda dt: datetime.datetime.strptime(dt, '%Y-%m-%d') - datetime.datetime.strptime('2020-01-21', '%Y-%m-%d')).apply(lambda x: str(x).split()[0]).astype(int)
test_df['Days'] = days_df
all_countries = train_df['Country'].unique()
ww_df = train_df.groupby('Date')[['ConfirmedCases', 'Fatalities']].sum().reset_index()
ww_df['new_case'] = ww_df['ConfirmedCases'] - ww_df['ConfirmedCases'].shift(1)
ww_df['new_deaths'] = ww_df['Fatalities'] - ww_df['Fatalities'].shift(1)
country_df = train_df.groupby(['Date', 'Country'])[['ConfirmedCases', 'Fatalities']].sum().reset_index()
target_date = country_df['Date'].max()
train_end_day = train_df['Days'].max()
test_start_day = test_df['Days'].min()
test_end_day = test_df['Days'].max()

py.init_notebook_mode()
top_country_df = country_df.query('(Date == @target_date) & (ConfirmedCases > 2000)').sort_values('ConfirmedCases', ascending=False)
print(len(top_country_df))
top_country_melt_df = pd.melt(top_country_df, id_vars='Country', value_vars=['ConfirmedCases', 'Fatalities'])
display(top_country_df.head())
display(top_country_melt_df.head())
fig = px.bar(top_country_melt_df.iloc[::-1],
             x='value', y='Country', color='variable', barmode='group',
             title=f'Confirmed Cases/Deaths on {target_date}', text='value', height=1500, orientation='h')
fig.show()

country_province_df = train_df[train_df['Country'] == 'United States'].groupby(['Date', 'Province_State'])[['ConfirmedCases', 'Fatalities']].sum().reset_index()
top_province_df = country_province_df.query('(Date == @target_date)').sort_values('ConfirmedCases', ascending=False)
top30_provinces = top_province_df.sort_values('ConfirmedCases', ascending=False).iloc[:30]['Province_State'].unique()
country_province_df['prev_cases'] = country_province_df.groupby('Province_State')['ConfirmedCases'].shift(1)
country_province_df['New Case'] = country_province_df['ConfirmedCases'] - country_province_df['prev_cases']
country_province_df['New Case'].fillna(0, inplace=True)
country_province_df['prev_deaths'] = country_province_df.groupby('Province_State')['Fatalities'].shift(1)
country_province_df['New Death'] = country_province_df['Fatalities'] - country_province_df['prev_deaths']
country_province_df['New Death'].fillna(0, inplace=True)
for province in top30_provinces:
    present_country_df = country_province_df[country_province_df['Province_State'] == province]

top30_countries = top_country_df.sort_values('ConfirmedCases', ascending=False).iloc[:30]['Country'].unique()
display(country_df[:20])
country_df['prev_cases'] = country_df.groupby('Country')['ConfirmedCases'].shift(1)
country_df['New Case'] = country_df['ConfirmedCases'] - country_df['prev_cases']
country_df['New Case'].fillna(0, inplace=True)
country_df['prev_deaths'] = country_df.groupby('Country')['Fatalities'].shift(1)
country_df['New Death'] = country_df['Fatalities'] - country_df['prev_deaths']
country_df['New Death'].fillna(0, inplace=True)
top30_country_df = country_df[country_df['Country'].isin(top30_countries)]
display(country_df[:10])
for country in top30_countries:
    present_country_df = top30_country_df[top30_country_df['Country'] == country]
    px.bar(present_country_df, x='Date', y='New Case', color='Country', title=f'DAILY NEW Confirmed cases in ' + country).show()