Skip to content
Advertisement

Python: How to add groupby but not affect ngroup()?

per user I want an unique item order (as they click through them). If a item already has been seen, then don’t cumulative count, but place the already assigned value there. For example, c,d, g & b in the tables below. I used the function below, but its not getting the job done at the moment. If I add the ‘user_id’ to the grouper I mess up the ngroup(). Can anyone help me with this?

JavaScript

Current Output:

JavaScript

Desired Output:

JavaScript

Advertisement

Answer

Use GroupBy.transform with factorize in lambda function:

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