Skip to content
Advertisement

Pandas Cumcount() over multiple columns

I have a dataframe that looks like this:

JavaScript
JavaScript

What I want to do is to add two columns, one for set number and one for rep number. Set number should increase by 1 for each weight change exercise & session is the same, else reset to 0.

Rep number should increase by 1 for each velocity change if exercise, session & weight is the same, else reset to 0.

The logic I wrote above was flawed. What I meant was that the set number should increase for each change in weight (per line), but reset to 0 if either exercise or session changes.

Rep count should then be the number of lines in each set.

Like this:

JavaScript
JavaScript

I think this should be possible using groupby and cumcount, but I struggle to make it work.

Advertisement

Answer

After experimenting a bit, I ended up using this solution:

JavaScript

It works as expected for the posted example and in my real dataset with much more data and more scenarios.

JavaScript
JavaScript
Advertisement