w3resource

Python Exercise: Get the Chinese province wise cases of confirmed, deaths and recovered cases of Novel Coronavirus

Python Project: COVID-19 Exercise-4 with Solution

Write a Python program to get the Chinese province wise cases of confirmed, deaths and recovered cases 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-17-2020.csv')
c_data = covid_data[covid_data['Country/Region']=='China']
c_data = c_data[['Province/State', 'Confirmed', 'Deaths', 'Recovered']]
result = c_data.sort_values(by='Confirmed', ascending=False)
result = result.reset_index(drop=True)
print(result)

Sample Output:

Dataset information:
<class 'pandas.core.frame.DataFrame'>
    Province/State  Confirmed  Deaths  Recovered
0            Hubei      67799    3111      56003
1        Guangdong       1364       8       1307
2            Henan       1273      22       1250
3         Zhejiang       1232       1       1216
4            Hunan       1018       4       1014
5            Anhui        990       6        984
6          Jiangxi        935       1        934
7         Shandong        761       7        746
8          Jiangsu        631       0        631
9        Chongqing        576       6        570
10         Sichuan        540       3        520
11    Heilongjiang        482      13        456
12         Beijing        456       8        369
13        Shanghai        358       3        325
14           Hebei        318       6        310
15          Fujian        296       1        295
16         Guangxi        253       2        248
17         Shaanxi        246       3        236
18          Yunnan        176       2        172
19          Hainan        168       6        161
20       Hong Kong        162       4         88
21         Guizhou        147       2        144
22         Tianjin        136       3        133
23           Gansu        133       2         91
24          Shanxi        133       0        133
25        Liaoning        125       1        120
26           Jilin         93       1         92
27        Xinjiang         76       3         73
28  Inner Mongolia         75       1         73
29         Ningxia         75       0         75
30         Qinghai         18       0         18
31           Macau         12       0         10
32           Tibet          1       0          1

Python Code Editor:

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

Previous: Write a Python program to get the latest number of confirmed deaths and recovered people of Novel Coronavirus (COVID-19) cases Country/Region - Province/State wise.
Next: Write a Python program to get the latest country wise deaths cases of Novel Coronavirus (COVID-19).

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

F strings:

It is a common practice to add variables inside strings. F strings are by far the coolest way of doing it. To appreciate the f strings more, let's first perform the operation with the format function.

name = 'Owen'
age = 25
print("{} is {} years old".format(name, age))

Output:

Owen is 25 years old

We specify the variables that go inside the curly braces by using the format function at the end. F strings allow for specifying the variables inside the string.

name = 'Owen'
age = 25
print(f"{name} is {age} years old")

Output:

Owen is 25 years old

F strings are easier to follow and type. Moreover, they make the code more readable.

A, B, C = {2, 4, 6}
print(A, B, C)
A, B, C = ['p', 'q', 'r']
print(A, B, C)

Output:

2 4 6
p q r