Assume I have a following dataframe:
date group value
2022-11-01. 1 4
2022-11-02. 1 12
2022-11-03. 1 14
2022-11-04. 1 25
2021-11-01. 2 9
2021-11-02. 2 7
2019-10-01. 3 40
2022-10-02. 3 14
I want to create a new column that is increasing integer based on date for each group. For example, this is the desired output:
date group value new_col
2022-11-01. 1 4. 0
2022-11-02. 1 12. 1
2022-11-03. 1 14. 2
2022-11-04. 1 25. 3
2021-11-01. 2 9. 0
2021-11-02. 2 7. 1
2019-10-01. 3 40. 0
2022-10-02. 3 14. 1
You see new_col is like np,arange(0, len(df['date'])+1)
, however I want to do it per group and it seems no variation of groupby works for me.
I have tried:
df.groupby('group')['date'].apply(lambda x: np.arange(0, len(x)+1)
However this is not even close to what I want. I would appreciate if someone explains how to do this correctly.