1

I would like to create a new column with the enumeration of Objects column based on membership in Name

Name Objects
A phone
B phone
B keys
B chewing gum
C radio
C chewing gum

into

Name Object no. Objects
A 1 phone
B 1 phone
B 2 keys
B 3 chewing gum
C 1 radio
C 2 chewing gum

I suppose that there should be a really easy way to do it

1 Answers1

2

You can try groupy then cumcount

df['Object no.'] = df.groupby('Name').cumcount().add(1)
print(df)

  Name      Objects  Object no.
0    A        phone           1
1    B        phone           1
2    B         keys           2
3    B  chewing gum           3
4    C        radio           1
5    C  chewing gum           2
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52