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

Creates a dictionary with the same keys as the provided dictionary and values generated by running the provided function for each value:

Example:

def tips_map_values(obj, fn):
  ret = {}
  for key in obj.keys():
    ret[key] = fn(obj[key])
  return ret
users = {
  'Owen': { 'user': 'Owen', 'age': 29 },
  'Eddie': { 'user': 'Eddie', 'age': 15 }
}

print(tips_map_values(users, lambda u : u['age'])) # {'Owen': 29, 'Eddie': 15}

Output:

{'Owen': 29, 'Eddie': 15}