简体   繁体   中英

Find largest 2 values for each year in the returned pandas groupby object after sorting each group

My dataframe has 3 columns: Year. Leading Cause,Deaths. I want to find the total number of deaths by leading cause in each year. I did the following: totalDeaths_Cause = df.groupby(["Year", "Leading Cause"])["Deaths"].sum() which results in:

 The total number of deaths for: Year Leading Cause 2009 Hypertension 26 2010 All Other Causes 2140 2011 Cerebrovascular Disease 281 Immunodeficiency 70 Parkinson Disease 180 2012 Cerebrovascular Disease 102 Disease1 183 Diseases of Heart 76 2013 Cerebrovascular Disease 386 Parkinson Disease 372 Self-Harm 17 Name: Deaths, dtype: int64

Now I want to get the largest 2 values(for deaths) each year and the leading Cause such that:

 The total number of deaths for: Year Leading Cause 2009 Hypertension 26 2010 All Other Causes 2140 2011 Cerebrovascular Disease 281 Parkinson Disease 180 2012 Disease1 183 Cerebrovasular disease 102 2013 Cerebrovascular Disease 386 Parkinson Disease 372

Thanks in advance for your help!

Let us do

df=df.sort_values().groupby(level=0).tail(1)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM