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