Difference makes the DIFFERENCE
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use(['dark_background'])
import seaborn as sns
sns.set(color_codes = True)
import json
import urllib.request
url = 'https://api.covid19india.org/states_daily.json'
urllib.request.urlretrieve(url, 'data.json')
with open('data.json') as f:
data = json.load(f)
data = data['states_daily']
df = pd.json_normalize(data)
df
df.head()
df.drop('dateymd', inplace=True, axis=1)
df.drop('tt', inplace = True, axis=1)
df.drop('date', axis=1, inplace=True)
df.set_index('status', inplace = True)
df.head()
df = df.apply(pd.to_numeric)
df.head()
df.info()
df.describe()
df_ = df.tail(3)
df_
df_ = df_.T
df_
df_.head()
plt.bar(df_.index, df_.Confirmed)
plt.bar(df_.index, df_.Deceased)
plt.bar(df_.index, df_.Recovered)
plt.bar(df_.index, df_.Confirmed)
plt.xticks(rotation=90);
plt.bar(df_.index, df_.Confirmed)
plt.bar(df_.index, df_.Recovered, bottom=df_.Confirmed)
plt.bar(df_.index, df_.Deceased, bottom=df_.Confirmed + df_.Recovered)
plt.xticks(rotation=90);
fig = plt.gcf()
fig.set_size_inches(16, 6)
plt.bar(df_.index, df_.Confirmed)
plt.bar(df_.index, df_.Recovered, bottom=df_.Confirmed)
plt.bar(df_.index, df_.Deceased, bottom=df_.Confirmed + df_.Recovered)
plt.xticks(rotation=90);
df_sum = df_.sum()
df_sum
fig = plt.gcf()
fig.set_size_inches(16, 6)
plt.bar(df_.index, df_.Confirmed, color = 'Orange')
plt.bar(df_.index, df_.Recovered, bottom=df_.Confirmed, color = "Green")
plt.bar(df_.index, df_.Deceased, bottom=df_.Confirmed + df_.Recovered, color = "Red")
plt.xticks(rotation=90);
for i, val in enumerate(df_.index): print(i, val)
fig = plt.gcf()
fig.set_size_inches(16, 6)
plt.bar(df_.index, df_.Confirmed, color = 'Orange')
plt.bar(df_.index, df_.Recovered, bottom=df_.Confirmed, color = "Green")
plt.bar(df_.index, df_.Deceased, bottom=df_.Confirmed + df_.Recovered, color = "Red")
plt.xticks(rotation=90);
for i, val in enumerate(df_.index):
y = df_.loc[val].sum()
x = i
plt.text(x, y, str(y))
fig = plt.gcf()
fig.set_size_inches(16, 6)
plt.bar(df_.index, df_.Confirmed, color = 'Orange')
plt.bar(df_.index, df_.Recovered, bottom=df_.Confirmed, color = "Green")
plt.bar(df_.index, df_.Deceased, bottom=df_.Confirmed + df_.Recovered, color = "Red")
plt.xticks(rotation=90);
for i, val in enumerate(df_.index):
y = df_.loc[val].sum() + 100
x = i
plt.text(x, y, str(y), ha='center');
fig = plt.gcf()
fig.set_size_inches(16, 6)
plt.bar(df_.index, df_.Confirmed, color = 'Orange')
plt.bar(df_.index, df_.Recovered, bottom=df_.Confirmed, color = "Green")
plt.bar(df_.index, df_.Deceased, bottom=df_.Confirmed + df_.Recovered, color = "Red")
plt.xticks(rotation=90);
for i, val in enumerate(df_.index):
y = df_.loc[val].sum() + 100
if y > 500:
x = i
plt.text(x, y, str(y), ha='center');
df_
df_['Total'] = 0
df_
df_.head(2)
# df_["Total"] = df_.Confirmed + df_.Recovered + df_.Deceased
# or use the following simple code
df_.Total = df_.sum(axis = 1)
df_.head()
df_['ConFrac'] = df_.Confirmed / df_.Total
df_['RecFrac'] = df_.Recovered / df_.Total
df_['DecFrac'] = df_.Deceased / df_.Total
df_.head(2)
df_.drop('ConfFrac', inplace=True, axis=1)
df_.head()
fig = plt.gcf()
fig.set_size_inches(16, 6)
plt.bar(df_.index, df_.ConFrac, color = 'Orange')
plt.bar(df_.index, df_.RecFrac, bottom=df_.ConFrac, color = "Green")
plt.bar(df_.index, df_.DecFrac, bottom=df_.ConFrac + df_.RecFrac, color = "Red")
plt.xticks(rotation=90);
df_ = df_.sort_values('ConFrac', ascending=False)
fig = plt.gcf()
fig.set_size_inches(16, 6)
plt.bar(df_.index, df_.ConFrac, color = 'Orange')
plt.bar(df_.index, df_.RecFrac, bottom=df_.ConFrac, color = "Green")
plt.bar(df_.index, df_.DecFrac, bottom=df_.ConFrac + df_.RecFrac, color = "Red")
plt.xticks(rotation=90);
df_ = df_.sort_values('Total', ascending=False)
fig = plt.gcf()
fig.set_size_inches(16, 6)
plt.bar(df_.index, df_.Confirmed, color = 'Orange')
plt.bar(df_.index, df_.Recovered, bottom=df_.Confirmed, color = "Green")
plt.bar(df_.index, df_.Deceased, bottom=df_.Confirmed + df_.Recovered, color = "Red")
plt.xticks(rotation=90);
for i, val in enumerate(df_.index):
y = df_.loc[val, "Total"] + 100
if y > 500:
x = i
plt.text(x, y, str(y), ha='center');
df_ = df_.sort_values('Total', ascending=False)
fig = plt.gcf()
fig.set_size_inches(16, 16)
plt.barh(df_.index, df_.Confirmed, color = 'Orange')
plt.barh(df_.index, df_.Recovered, left=df_.Confirmed, color = "Green")
plt.barh(df_.index, df_.Deceased, left=df_.Confirmed + df_.Recovered, color = "Red")
plt.xticks(rotation=90);