I have a data frame like as shown below
import pandas as pd
import numpy as np
df=pd.DataFrame({'Adm DateTime':['02/25/2012','03/05/1996','11/12/2010','31/05/2012','21/07/2019','31/10/2020'],
's_id':[1,1,1,1,2,2],
't_id':['t1','t2','t3','t3','t4','t5']})
Please note that I would like to achieve this using the group by approach. While I already referred to this post, I cannot use this solution for my data (as there is a need to use group by)
I would like to generate a row number for each of the rows based on each subject. Though I am able to do that successfully using the below code there is a minor issue with the output
df['op'] = df.sort_values(['Adm DateTime'], ascending=True).groupby(['s_id']).cumcount() + 1
But the below code produces output like as shown below (which is slightly incorrect)
However, I expect my output to be like as shown below

