Pandas & matplotlib: What is the best way to sort a dataframe that’s been grouped, by the total sum of the grouped sums

  dataframe, matplotlib, pandas, python

I’ve searched through SO and can’t seem to find a question that quite covers my particular case (if I’ve missed it – please let me know!)

I’ve got data structured like the image below:

Data structure

There’s a lot of data so I need to avoid creating new columns (not if there’s a more efficient way!)

I’ve grouped it by ‘Name’ and ‘Result’ and used Size():

df = df.groupby([‘Name’,’Result’]).size().sort_values(ascending = False)

which sorts simply on the ‘Name’ column giving me this (and the graph below when I plot it using matplotlib)

Grouped df
grouped df bar chart plot

The dataframe is sorted by the ‘Name’ column. How would I sort it by the sum of each Name’s grouped values (descending order). In this case since Sandy has 4 results, Adam 3, Meg 3 and Toby 2:

sorted grouped df

And which would consquently plot like this:

sorted grouped df bar chart plot

Source: Python Questions

LEAVE A COMMENT