Creating a order column based on datetime in dataframe

Question:

UserID  Datetime  Object  Order*
A       1/2/20    Apple   1     
A       4/4/20    Banana  3     
A       3/3/20    Pear    2     
B       7/6/20    Pear    3     
B       5/6/20    Banana  2      
B       2/2/20    Apple   1      

I have a dataframe that looks similar to this table but without the Order column. I want to create this order column based on the Datetime to look at average position of the Object column. The problem I am running into is figuring out how to subset the Users within the dataframe.

Asked By: Nick P

||

Answers:

You need sort_values and cumcount:

#ensure your datetime is a proper datetime 
#df['Datetime']  = pd.to_datetime(df['Datetime'])
df['Order'] = df.sort_values('Datetime').groupby('UserID').cumcount() + 1


  UserID   Datetime  Object  Order*  Order
0      A 2020-01-02   Apple       1      1
1      A 2020-04-04  Banana       3      3
2      A 2020-03-03    Pear       2      2
3      B 2020-07-06    Pear       3      3
4      B 2020-05-06  Banana       2      2
5      B 2020-02-02   Apple       1      1
Answered By: Umar.H
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.