A user U1 moves through the zones Z1, Z2, Z3 at time t1, t2, t3
A user U1 goes back and forth through the zones Z1, Z2 at t1, t2,t3, t4
This is what I call a user « OSCILLATING ».

This is considered as an oscillation: the user U1 goes from Z1 to Z2 and then to Z1. The user visits Z1 more than one Time eventhough he visited Z2 only once.
Z1 ==> Z2 ==> Z1
The user U1 goes from Z1 to Z3 then to Z2, Z3 and Z1, respectively at time t1,t2, t3,t4 t5.
The user is oscillation between the 3 zones.
As for the previous example, we consider this movement as an oscillation because the user visits and Z1 and Z3 more than one time eventhough he only visited Z2 only once.
For ease of computation we can set the number of zones max that a user oscillates in to 5 zones.
I would like to give to create a column that tracks the oscillation.
For a given user, if he is oscillating, give the rows the same oscillating ID.
If there is no oscillation, set it to NULL or set it to 0
For example :
Example DATA to copy/paste:
Zone, time, person, Oscillation_ID
A, 1, ABC, 1
B, 2, ABC, 1
A, 3, ABC, 1
A, 4, ABC, 1
B, 5, ABC, 1
A, 6, ABC, 1
C, 7, ABC, 2
D, 8, ABC, 2
E, 9, ABC, 2
C, 10, ABC, 2
E, 11, ABC, 2
D, 12, ABC, 2
C, 13, ABC, 2
C, 14, ABC, 2
D, 15, ABC, 2
E, 16, ABC, 2
C, 17, ABC, 2
Z, 18, ABC, 3
X, 19, ABC, 4
Y, 20, ABC, 5
Because I am working with billions of records I would need an efficient solution.
I am using Spark 2.3
I accept both scala and python (pyspark) solutions.



