Python Bitmap Ranking Series

I have a data frame with more than 2 columns like this

columnA columnB
    1      a
    1      b
    1      c
    2      d

      

I just want to rank columnA like this

columnA columnB rankA
    1      a      1
    1      b      2
    1      c      3
    2      d      1

      

So what should I do?

+3


source to share


1 answer


Use groupby

+ cumcount

:



df['rankA'] = df.groupby('columnA').cumcount() + 1
print (df)
   columnA columnB  rankA
0        1       a      1
1        1       b      2
2        1       c      3
3        2       d      1

      

0


source







All Articles