I noticed that there are several uses for pd.DataFrame.groupby followed by apply , implying that groupby is stable - that is, if a and b are instances of the same group and are pre-grouped, a appears before b, then a will be displayed pre b after grouping,
I think there are several answers that explicitly use this, but to be specific, here one uses groupby + cumsum .
Is there anything really promising this kind of behavior? The documentation only states:
A group series using mapper (a dict or key function, applies this function to a group, returns the result as a series) or a series of columns.
Besides pandas with indexes, it would theoretically be possible to implement functionality without this guarantee (albeit more cumbersome).
python language-lawyer pandas group-by
Ami tavory
source share