I have a dataframe df:
Name | Place | Price |
---|---|---|
Bob | NY | 15 |
Jack | London | 27 |
John | Paris | 5 |
Bill | Sydney | 3 |
Bob | NY | 39 |
Jack | London | 9 |
Bob | NY | 2 |
Dave | NY | 7 |
I need to assign an incremental value (from 1 to N) for each row which has the same name and place (price can be different).
df_out:
Name | Place | Price | Value |
---|---|---|---|
Bob | NY | 15 | 1 |
Jack | London | 27 | 1 |
John | Paris | 5 | 1 |
Bill | Sydney | 3 | 1 |
Bob | NY | 39 | 2 |
Jack | London | 9 | 2 |
Bob | NY | 2 | 3 |
Dave | NY | 7 | 1 |
I could do this by sorting the dataframe (on Name and Place) and then iteratively checking if they match between two consecutive rows. Is there a smarter/faster pandas way to do this?