pythonpandasrankingrank

Assign rank for each group


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


Solution

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

    df['date_rank'] = df.groupby(['CookieID'])['PageViewDate'].cumcount() + 1