1

need help. i have a sample data which contains sessionid and datetime visited. one session may be visited multiple pages in the same day. i need to assign ranking for each date group by the session.

sample enter image description here

the code which i am using is

   df['date_rank'] = df.groupby(['CookieID'])['PageViewDate'].rank().astype(int)

but it is not giving expected rank

the output is

enter image description here

sen
  • 51
  • 1
  • 7

1 Answers1

1

We can try to use the cumcount() method instead of rank() :

df['date_rank'] = df.groupby(['CookieID'])['PageViewDate'].cumcount() + 1
tlentali
  • 3,407
  • 2
  • 14
  • 21