Assign rank for each group by each date in pandas data frame – not getting expected rank value

Question:

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

Asked By: sen

||

Answers:

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

df['date_rank'] = df.groupby(['CookieID'])['PageViewDate'].cumcount() + 1
Answered By: tlentali
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.