0

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?

Vineet
  • 33
  • 4

1 Answers1

1

You can use a grouped (on Name, Place) cumulative count and add 1 as it starts from 0:

df['Value'] = df.groupby(['Name','Place']).cumcount().add(1)

prints:

   Name   Place  Price  Value
0   Bob      NY     15      1
1  Jack  London     27      1
2  John   Paris      5      1
3  Bill  Sydney      3      1
4   Bob      NY     39      2
5  Jack  London      9      2
6   Bob      NY      2      3
7  Dave      NY      7      1
sophocles
  • 13,593
  • 3
  • 14
  • 33