[Solved] How to compute the ratio of Recovered Cases to Confirmed Cases for each nation using pandas in 8 lines [closed]


Computing the ratio

Since there are multiple regions in a country, there are duplicated values in the Country_Region column. Therefore, I use groupby to sum the total cases of a nation.

ratio = df.groupby("Country_Region")[["Recovered", "Confirmed"]].sum()
ratio["Ratio"] = ratio["Recovered"] / ratio["Confirmed"]

Let’s get the first five nations.

>>> ratio.head()

                Recovered  Confirmed     Ratio
Country_Region                                
Afghanistan         41727      52513  0.794603
Albania             33634      58316  0.576754
Algeria             67395      99897  0.674645
Andorra              7463       8117  0.919428
Angola              11146      17568  0.634449

Getting the countries with the highest ratio

Then, you can filter out the ten countries with the highest ratio with Series.nlargest.

>>> ratio.nlargest(10, "Ratio")

                  Recovered  Confirmed     Ratio
Country_Region                                  
Marshall Islands          4          4  1.000000
Samoa                     2          2  1.000000
Vanuatu                   1          1  1.000000
Singapore             58449      58629  0.996930
El Salvador           45960      46515  0.988068
Qatar                141556     144042  0.982741
Djibouti               5735       5840  0.982021
Diamond Princess        699        712  0.981742
Gabon                  9388       9571  0.980880
Ghana                 53758      54930  0.978664

solved How to compute the ratio of Recovered Cases to Confirmed Cases for each nation using pandas in 8 lines [closed]