Is there a way of labelling data sequentially in python by group?

Viewed 23

I have a dataset grouped by an ID, but I want to add a new label according to the date the ID occurred.

My dataset looks like this:

ID Date
1 10/11/20
1 15/11/20
1 13/12/20
1 16/12/20
2 10/11/19
2 15/11/19
2 13/12/20
2 16/12/20
3 10/11/19
3 15/11/19
3 13/12/20
3 16/12/20
4 10/11/19
4 15/11/19
4 13/12/20
4 16/12/20

And so on.

I would like my dataset, when ordered, to then look like this:

ID Date Order
1 10/11/20 1
1 15/11/20 2
1 13/12/20 3
1 16/12/20 4
2 10/11/19 1
2 15/11/19 2
2 13/12/20 3
2 16/12/20 4
3 10/11/19 1
3 15/11/19 2
3 13/12/20 3
3 16/12/20 4
4 10/11/19 1
4 15/11/19 2
4 13/12/20 3
4 16/12/20 4

Does anybody know if there is a way to do this or a function that could help?

0 Answers
Related