Sorting the grouped data as per group size in Pandas

nishant picture nishant · Mar 10, 2014 · Viewed 22k times · Source

I have two columns in my dataset, col1 and col2. I want group the data as per col1 and then sort the data as per the size of each group. That is, I want to display groups in ascending order of their size.

I have written the code for grouping and displaying the data as follows:

grouped_data = df.groupby('col1')
"""code for sorting comes here"""
for name,group in grouped_data:
          print (name)
          print (group)

Before displaying the data, I need to sort it as per group size, which I am not able to do.

Answer

Victor Yan picture Victor Yan · Jun 12, 2015

For Pandas 0.17+, use sort_values:

df.groupby('col1').size().sort_values(ascending=False)

For pre-0.17, you can use size().order():

df.groupby('col1').size().order(ascending=False)