vinotkr
vinotkr

Reputation: 99

Pandas - Ranking items within group by treating repeat items in a group as new

Dataframe has two columns (ColA & ColB)

 ColA   ColB
 123     A
 123     B
 123     C
 123     C
 123     D
 123     C
 123     C

 456     A
 456     B
 456     D
 456     D
 456     E

I would like to create a new Column that ranks within the group by treating repeat items within a group as a new item (Ex: To rank item C twice in group 123). Looking for a solution that should be like:

ColA    ColB    ColC
 123     A       1
 123     B       2
 123     C       3
 123     C       3
 123     D       4
 123     C       5
 123     C       5

 456     A       1
 456     B       2
 456     D       3
 456     D       3
 456     E       4

Thanks

Upvotes: 3

Views: 58

Answers (1)

sacuL
sacuL

Reputation: 51395

You can do:

df['ColC'] = df.groupby('ColA')['ColB'].transform(lambda x:(x!=x.shift()).cumsum())

>>> df
    ColA ColB  ColC
0    123    A     1
1    123    B     2
2    123    C     3
3    123    C     3
4    123    D     4
5    123    C     5
6    123    C     5
7    456    A     1
8    456    B     2
9    456    D     3
10   456    D     3
11   456    E     4

Upvotes: 3

Related Questions