Skip to content Skip to sidebar Skip to footer

Groupby Preserve Order Among Groups? In Which Way?

While answering a question Sort a pandas's dataframe series by month name? we meet some weird behavior of groupby. df = pd.DataFrame([['dec', 12], ['jan', 40], ['mar', 11], ['aug',

Solution 1:

pandas.DataFrame.groupby has a sort argument that defaults to True. Try

total = (df.groupby(df['Month'], sort=False)['Price'].mean())

Post a Comment for "Groupby Preserve Order Among Groups? In Which Way?"