Bhavesh Parvatkar
Bhavesh Parvatkar

Reputation: 2233

Sorting grouped DataFrame column without changing index sorting

I have a df as below:

df

I want only the top 5 countries from each year but keeping the year ascending. First I grouped the df by year and country name and then ran the following code:

df.sort_values(['year','hydro_total'], ascending=False).groupby(['year']).head(5)

The result didn't keep the index ascending, instead, it sorted the year index too. How do I get the top 5 countries and keep the year's group ascending?

enter image description here

The CSV file is uploaded HERE .

Upvotes: 1

Views: 35

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

You already sort by year and hydro_total, both decreasingly. You need to sort the year as increasing:

(df.sort_values(['year','hydro_total'], 
                ascending=[True,False])
   .groupby('year').head(5)
)

Output:

          country  year  hydro_total  hydro_per_person
440         Japan  1971    7240000.0           0.06890
160         China  1971    2580000.0           0.00308
240         India  1971    2410000.0           0.00425
760   North Korea  1971     788000.0           0.05380
800      Pakistan  1971     316000.0           0.00518
...           ...   ...          ...               ...
199         China  2010   62100000.0           0.04630
279         India  2010    9840000.0           0.00803
479         Japan  2010    7070000.0           0.05590
1119       Turkey  2010    4450000.0           0.06120
839      Pakistan  2010    2740000.0           0.01580

Upvotes: 1

Related Questions