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

Time:09-28

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)  

CodePudding user response:

# 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
  • Related