Using Plotly’s chloropeth graphs and generic python graphing to visualize Covid 19 infection and death rates and the impact of lockdown in various countries.
Last updated on
Oct 24, 2021
Covid 19 analysis using Python
We use Python to animate the spread of covid around the world. Then we focus on a few countries and see how the impact of lockdown has affected the spread of covid in that country. We further see how the infection rates and death rates are correlated.
Importing modules
Task 1
import pandas as pd
import numpy as np
import plotly.express as px
import matplotlib.pyplot as plt
print('modules are imported')
#log to increase the quALITY FOR low bars - changes scale for y axis
Task 4: Let’s See how National Lockdowns Impacts Covid19 transmission in Italy
COVID19 pandemic lockdown in Italy
On 9 March 2020, the government of Italy under Prime Minister Giuseppe Conte imposed a national quarantine, restricting the movement of the population except for necessity, work, and health circumstances, in response to the growing pandemic of COVID-19 in the country. source
/var/folders/43/4nqhk6qx3kxcwf85q5ncg9lm0000gn/T/ipykernel_74583/3001688291.py:1: SettingWithCopyWarning:
A valueis trying to be seton a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
Date
Country
Confirmed
Recovered
Deaths
Infection Rate
44880
2020-01-22
Italy
0
0
0
NaN
44881
2020-01-23
Italy
0
0
0
0.0
44882
2020-01-24
Italy
0
0
0
0.0
44883
2020-01-25
Italy
0
0
0
0.0
44884
2020-01-26
Italy
0
0
0
0.0
ok! now let’s do the visualization
FigIt=px.line(df_italy, x='Date', y='Infection Rate', title="Before and After lockdown in Italy")
FigIt.show()
FigIt2=px.line(df_italy, x='Date', y='Infection Rate', title="Before and After lockdown in Italy")
FigIt2.add_shape(
dict(
type="line",
x0=italy_lockdown_start_date,
y0=0,
x1=italy_lockdown_start_date,
y1=df_italy['Infection Rate'].max(),
line=dict(color='red', width=2)
)
)
FigIt2.add_annotation(
dict(
x=italy_lockdown_start_date,
y=df_italy['Infection Rate'].max(),
text='Starting Date of Lockdown'
)
)
FigIt2.show()
FigIt3=px.line(df_italy, x='Date', y='Infection Rate', title="Before and After lockdown in Italy")
FigIt3.add_shape(
dict(
type="line",
x0=italy_lockdown_start_date,
y0=0,
x1=italy_lockdown_start_date,
y1=df_italy['Infection Rate'].max(),
line=dict(color='red', width=2)
)
)
FigIt3.add_annotation(
dict(
x=italy_lockdown_start_date,
y=df_italy['Infection Rate'].max(),
text='Starting Date of Lockdown'
)
)
FigIt3.add_shape(
dict(
type="line",
x0=italy_lockdown_a_month_later,
y0=0,
x1=italy_lockdown_a_month_later,
y1=df_italy['Infection Rate'].max(),
line=dict(color='red', width=2)
)
)
FigIt3.add_annotation(
dict(
x=italy_lockdown_a_month_later,
y=4000,
text='One month post Lockdown'
)
)
FigIt3.show()
Task 5: Let’s See how National Lockdowns Impacts Covid19 active cases in Italy
df_italy.head()
Date
Country
Confirmed
Recovered
Deaths
Infection Rate
44880
2020-01-22
Italy
0
0
0
NaN
44881
2020-01-23
Italy
0
0
0
0.0
44882
2020-01-24
Italy
0
0
0
0.0
44883
2020-01-25
Italy
0
0
0
0.0
44884
2020-01-26
Italy
0
0
0
0.0
let’s calculate number of active cases day by day
df_italy['Death Rate']=df_italy.Deaths.diff()
/var/folders/43/4nqhk6qx3kxcwf85q5ncg9lm0000gn/T/ipykernel_74583/834131105.py:1: SettingWithCopyWarning:
A valueis trying to be seton a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
let’s check the dataframe again
df_italy.head()
Date
Country
Confirmed
Recovered
Deaths
Infection Rate
Death Rate
44880
2020-01-22
Italy
0
0
0
NaN
NaN
44881
2020-01-23
Italy
0
0
0
0.0
0.0
44882
2020-01-24
Italy
0
0
0
0.0
0.0
44883
2020-01-25
Italy
0
0
0
0.0
0.0
44884
2020-01-26
Italy
0
0
0
0.0
0.0
now let’s plot a line chart to compare COVID19 national lockdowns impacts on spread of the virus and number of active cases
Absolute Death Rates and Infection Rates Before and After Lockdown - not easily comparable
df_italy['N Infection Rate']=df_italy['Infection Rate']/df_italy['Infection Rate'].max()
df_italy['N Death Rate']=df_italy['Death Rate']/df_italy['Death Rate'].max()
/var/folders/43/4nqhk6qx3kxcwf85q5ncg9lm0000gn/T/ipykernel_74583/3675118474.py:1: SettingWithCopyWarning:
A valueis trying to be seton a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
/var/folders/43/4nqhk6qx3kxcwf85q5ncg9lm0000gn/T/ipykernel_74583/3675118474.py:2: SettingWithCopyWarning:
A valueis trying to be seton a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
figf= px.line(df_italy, x='Date', y=['N Infection Rate', 'N Death Rate'])
figf.show()
My research interests include aspects of universal health coverage, health systems, health promotion and health inequalities with a focus at oral health and sugar