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?



Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

Decompose a collection:

Assume we have a function that returns a tuple of two values and we want to assign each value to a separate variable. One way is to use indexing as below:

abc = (5, 10)
x = abc[0]
y = abc[1]
print(x, y)

Output:

5 10

There is a better option that allows us to do the same operation in one line.

x, y = abc
print(x, y)

Output:

5 10

It can be extended to a tuple with more than 2 values or some other data structures such as lists or sets.

x, y, z = {1, 2, 3}
print(x, y, z)
x, y, z = ['a', 'b', 'c']
print(x, y, z)

Output:

1 2 3
a b c