Last active
July 8, 2020 06:28
-
-
Save albertochiwas/81934e61f7390040dd2c1d4bbce5ab7b to your computer and use it in GitHub Desktop.
Report total deaths by COVID-19 by country (TOP 10) using this dataset: https://covid.ourworldindata.org/data/owid-covid-data.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Python for Everybody / Dr. Chuck / Univ Michigan / Coursera | |
Course 5: "Capstone: Retrieving, Processing, and Visualizing Data with Python" | |
covid-top20.py: | |
Read COVID-19 JSON dataset | |
Add to a list the total deaths, name & date for each country | |
Finally, sort and report the Top 20 COVID-19 countries | |
Directions: | |
1. Download most recent JSON Dataset from: | |
https://covid.ourworldindata.org/data/owid-covid-data.json | |
2. Download the python script from: | |
https://gist.github.com/albertochiwas/81934e61f7390040dd2c1d4bbce5ab7b | |
3. Run Python script from the same dataset folder | |
python covid-top20.py | |
By Alberto Pacheco (alberto@acm.org), CC BY-NC-SA 4.0, 07/07/2020 | |
Dataset: https://covid.ourworldindata.org/data/owid-covid-data.json | |
''' | |
import json | |
# https://pypi.org/project/colorama | |
from colorama import Fore, Style, init | |
init() | |
# https://linuxconfig.org/how-to-parse-data-from-json-into-python | |
with open('owid-covid-data.json', 'r') as f: | |
countries_dict = json.load(f) | |
all = [] | |
for country in countries_dict.values(): | |
try: | |
name = country["location"] | |
last = country["data"][-1] | |
deaths = last["total_deaths"] | |
date = last["date"] | |
all.append( (int(deaths), name, date) ) | |
except: | |
pass | |
all = sorted(all, reverse=True) | |
world = all[0] | |
print() | |
print(" "*7, f"TOP 20 ({world[1]}: {world[0]:,d} deaths)") | |
print() | |
print(Fore.YELLOW + "-"*46) | |
print(Fore.CYAN + " # " + 'Country'.center(18) + | |
'Deaths'.center(8) + 'Date'.center(11)) | |
print(Fore.YELLOW + "-"*46) | |
for i, c in enumerate(all[1:11]): | |
print(Fore.RED + f"{i+1:2d}) {c[1]:>15s} {c[0]:9,d} ", | |
Style.DIM + c[2] + Style.RESET_ALL) | |
for i, c in enumerate(all[11:21]): | |
print(Style.DIM + Fore.MAGENTA + | |
f"{i+11:2d}) {c[1]:>15s} {c[0]:9,d} ", c[2]) | |
print() | |
print(Fore.WHITE + "Source: https://ourworldindata.org/coronavirus") | |
print(Fore.WHITE + " Code: Alberto Pacheco (alberto@acm.org)") | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment