groupby count sort_values code example Example: how to sort dataframe in python by length of groups df.groupby('col1').size().sort_values(ascending=False)