Skip to content
Advertisement

Add a sequential counter column on groups to a pandas dataframe

I feel like there is a better way than this:

JavaScript

To achieve this:

JavaScript

Is there a way to do it that avoids the callback?

Advertisement

Answer

use cumcount(), see docs here

JavaScript

If you want orderings starting at 1

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement