Q&A library Python

Why does groupby change the order of my results in pandas?

Python Intermediate groupbyperformancesorting

Short answer

groupby sorts the group keys by default, so the output order is alphabetical rather than the order the groups appeared. Pass sort=False to keep first-appearance order, which is also slightly faster.

The short version

groupby sorts the group keys by default, so your results come back in alphabetical or numeric order rather than the order the groups first appeared in the data. Pass sort=False to keep first-appearance order, which also saves the cost of the sort.

Seeing the reordering

import pandas as pd

df = pd.DataFrame({
    "key": ["z", "a", "z"],
    "value": [1, 2, 3],
})

df.groupby("key")["value"].sum().index.tolist()              # ['a', 'z']
df.groupby("key", sort=False)["value"].sum().index.tolist()  # ['z', 'a']

The data met z first, but the default output puts a first because it sorted the keys.

Why it matters

Usually the sorted order is harmless or even helpful. It matters when the original order carried meaning, for example when your rows were already arranged by time, size or a deliberate ranking, and you expected the groups to follow that order. It also matters when you are about to line the result up against another object that is in the original order, because the mismatch can silently misalign things.

The performance angle

Sorting the keys costs time. On a frame with many distinct groups, sort=False can be noticeably quicker, because pandas skips the sort entirely. If you are going to sort the result yourself afterwards anyway, or the order does not matter, turning it off is free speed.

df.groupby("key", sort=False)["value"].sum()

Sorting the result deliberately

If you want a specific order, produce it explicitly rather than relying on the grouping default. This is clearer to a reader, because the intent is visible.

df.groupby("key")["value"].sum().sort_values(ascending=False)

Sorting by the values (largest total first) is usually what a report actually wants, and that is different from sorting by the key, which is all the default gives you.

The interaction with categoricals

If the grouping key is an ordered category, groupby respects the category order rather than the alphabet, which is exactly why setting an ordered category is useful for scales like low, medium, high. That is a much better way to control group order than trying to sort strings into a meaningful sequence.

The general lesson

Several pandas operations quietly impose an order (groupby sorts keys, value_counts sorts by frequency, merge may not preserve row order). If your output order matters, do not rely on a default; state it. An explicit sort_values at the end of a pipeline costs one line and removes any doubt about what a reader, or a downstream step, will receive.

Need this solved on your own data?

The library is free and always will be. When the problem is bigger than an answer, we take on the work.

Start a project