Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Python Exercise: Get the top 10 countries data of Novel Coronavirus

Python Project: COVID-19 Exercise-9 with Solution

Write a Python program to get the top 10 countries data (Last Update, Country/Region, Confirmed, Deaths, Recovered) of Novel Coronavirus (COVID-19).

Sample Solution:

Python Code:

import pandas as pd
covid_data= pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/03-18-2020.csv', usecols = ['Last Update', 'Country/Region', 'Confirmed', 'Deaths', 'Recovered'])
result = covid_data.groupby('Country/Region').max().sort_values(by='Confirmed', ascending=False)[:10]
pd.set_option('display.max_column', None)
print(result)

Sample Output:

Dataset information:
&                        Last Update  Confirmed  Deaths  Recovered
Country/Region                                                   
China           2020-03-18T12:13:09      67800    3122      56927
Italy           2020-03-18T17:33:05      35713    2978       4025
Iran            2020-03-18T12:33:02      17361    1135       5389
Spain           2020-03-18T13:13:13      13910     623       1081
Germany         2020-03-18T19:33:02      12327      28        105
France          2020-03-18T18:33:02       9043     148         12
Korea, South    2020-03-18T02:53:03       8413      84       1540
Switzerland     2020-03-18T14:53:05       3028      28         15
United Kingdom  2020-03-18T14:53:05       2626      71         65
US              2020-03-18T19:53:03       2495      55        106

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to list countries with all cases of Novel Coronavirus (COVID-19) recovered.
Next: Write a Python program to create a plot (lines) of total deaths, confirmed, recovered and active cases Country wise where deaths greater than 150.

What is the difficulty level of this exercise?



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m