How do I sort a data frame by a single column, but for groups of another column?

Question:

I have the following data frame:

            Names      Counts  Year
0           Jordan        1043  2000
1            Steve         204  2000
2            Brock           3  2000
3            Steve          33  2000
4             Mike          88  2000
...           ...         ...   ...
20001        Bryce           2  2015
20002        Steve          11  2015
20003        Penny          24  2015
20004        Steve          15  2015
20005        Ryan            5  2015

I want to group the names by year, and then sort them in descending order of counts.

Here’s an example of what the output might look like:

            Names      Counts  Year
0           Jordan        1043  2000
1            Steve         204  2000
2             Mike          88  2000
3            Steve          33  2000
4            Brock           3  2000
...           ...         ...   ...
20001        Penny          24  2015
20002        Steve          15  2015
20003        Steve          11  2015
20004        Ryan            5  2015    
20005        Bryce           2  2015

What I’ve tried:

df = (df[df['Names'].groupby('Year').sort_values(by=['Counts'], ascending=False)  

Asked By: display_name_1

||

Answers:

# the list values under ascending are for 1 for ascending, and 0 for descending order
df.sort_values(['Year','Counts','Names'], ascending=[1,0,1])
       Names    Counts  Year
0       Jordan  1043    2000
1       Steve    204    2000
4       Mike      88    2000
3       Steve     33    2000
2       Brock      3    2000
20003   Penny     24    2015
20004   Steve     15    2015
20002   Steve     11    2015
20005   Ryan       5    2015
20001   Bryce      2    2015
Answered By: Naveed
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.