bipvan
bipvan

Reputation: 97

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!

Upvotes: 1

Views: 47

Answers (1)

BENY
BENY

Reputation: 323286

Let us do

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

Upvotes: 1

Related Questions