Looking at some COVID-19 data, I want to plot the number of deaths per country as a percentage.
At present, I can group by country and sum the total deaths like this:
ecdc.groupby("countriesAndTerritories")["deaths"].sum().sort_values(ascending = False).head(10).plot(kind = "bar")
This produces the following plot:
This is almost what I want, but I don't know how to go from here / if the way I am thinking of is possible. The other field that I want to use is popData2018
thus: deaths/popData2018
to give me the percentage of deaths per country.
At present, the US is leading the death toll at over 65,000 in total, but they are not the country with the largest percentage of deaths based on population (Belgium leads here) and I want my graph to reflect this.
How can I achieve this?
If you wish to reproduce my work:
import pandas as pd
ecdc = pd.read_csv("https://opendata.ecdc.europa.eu/covid19/casedistribution/csv")
You can use the following:
(ecdc.groupby('countriesAndTerritories').agg(
total_deaths=('deaths', 'sum'),
population=('popData2018', 'first')
).assign(perc=lambda x: x['total_deaths'] / x['population'])
.nlargest(10, 'perc')
.plot(kind='bar', y='perc')
)
Or for pandas < 0.25.0
we cannot use named aggregations
:
(ecdc.groupby('countriesAndTerritories').agg(
{'deaths':'sum',
'popData2018':'first'}
).assign(perc=lambda x: x['deaths'] / x['popData2018'])
.nlargest(10, 'perc')
.plot(kind='bar', y='perc')
)
Which takes the sum
of deaths and the popdata, then creates a perc
column and plots the top 10 highest percentage deaths
Or more broken down and not in a one-liner:
grps = ecdc.groupby('countriesAndTerritories').agg(
total_deaths=('deaths', 'sum'),
population=('popData2018', 'first')
).reset_index()
grps['perc'] = grps['total_deaths'] / grps['population']
grps.nlargest(10, 'perc').plot(kind='bar', x='countriesAndTerritories', y='perc')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.