I have a dataframe as follows:
+-------+----------+-----+
|user_id| date|valor|
+-------+----------+-----+
| 1|2022-01-01| 0|
| 1|2022-01-02| 0|
| 1|2022-01-03| 1|
| 1|2022-01-04| 1|
| 1|2022-01-05| 1|
| 1|2022-01-06| 0|
| 1|2022-01-07| 0|
| 1|2022-01-08| 0|
| 1|2022-01-09| 1|
| 1|2022-01-10| 1|
| 1|2022-01-11| 1|
| 1|2022-01-12| 0|
| 1|2022-01-13| 0|
| 1|2022-01-14| -1|
| 1|2022-01-15| -1|
| 1|2022-01-16| -1|
| 1|2022-01-17| -1|
| 1|2022-01-18| -1|
| 1|2022-01-19| -1|
| 1|2022-01-20| 0|
+-------+----------+-----+
The goal is to calculate a score for the user_id using valor as base, it will start from 3 and increase or decrease by 1 as it goes in the valor column. The main problem here is that my score can't be under 1 and can't be over 5, so the sum must always stay on the range and not lose the last value so I can compute it right. So what I expect is this:
+-------+----------+-----+-----+
|user_id| date|valor|score|
+-------+----------+-----+-----+
| 1|2022-01-01| 0| 3|
| 1|2022-01-02| 0| 3|
| 1|2022-01-03| 1| 4|
| 1|2022-01-04| 1| 5|
| 1|2022-01-05| 1| 5|
| 1|2022-01-06| 0| 5|
| 1|2022-01-07| 0| 5|
| 1|2022-01-08| 0| 5|
| 1|2022-01-09| 1| 5|
| 1|2022-01-10| -1| 4|
| 1|2022-01-11| -1| 3|
| 1|2022-01-12| 0| 3|
| 1|2022-01-13| 0| 3|
| 1|2022-01-14| -1| 2|
| 1|2022-01-15| -1| 1|
| 1|2022-01-16| 1| 2|
| 1|2022-01-17| -1| 1|
| 1|2022-01-18| -1| 1|
| 1|2022-01-19| 1| 2|
| 1|2022-01-20| 0| 2|
+-------+----------+-----+-----+
So far, I've done a window to rank the column valor, so I can keep track of the quantity of increases or decreases in sequence and remove from valor the sequences larger then 4, but I don't know how to keep the sum in valor_ in the range (1:5):
+-------+----------+----+-----+------+
|user_id| date|rank|valor|valor_|
+-------+----------+----+-----+------+
| 1|2022-01-01| 0| 0| 0|
| 1|2022-01-02| 0| 0| 0|
| 1|2022-01-03| 1| 1| 1|
| 1|2022-01-04| 2| 1| 1|
| 1|2022-01-05| 3| 1| 1|
| 1|2022-01-06| 0| 0| 0|
| 1|2022-01-07| 0| 0| 0|
| 1|2022-01-08| 0| 0| 0|
| 1|2022-01-09| 1| 1| 1|
| 1|2022-01-10| 2| 1| 1|
| 1|2022-01-11| 3| 1| 1|
| 1|2022-01-12| 0| 0| 0|
| 1|2022-01-13| 0| 0| 0|
| 1|2022-01-14| 1| -1| -1|
| 1|2022-01-15| 2| -1| -1|
| 1|2022-01-16| 3| -1| -1|
| 1|2022-01-17| 4| -1| -1|
| 1|2022-01-18| 5| -1| 0|
| 1|2022-01-19| 6| -1| 0|
As you can see, the result here is not what I expected:
+-------+----------+----+-----+------+-----+
|user_id| date|rank|valor|valor_|score|
+-------+----------+----+-----+------+-----+
| 1|2022-01-01| 0| 0| 0| 3|
| 1|2022-01-02| 0| 0| 0| 3|
| 1|2022-01-03| 1| 1| 1| 4|
| 1|2022-01-04| 2| 1| 1| 5|
| 1|2022-01-05| 3| 1| 1| 6|
| 1|2022-01-06| 0| 0| 0| 6|
| 1|2022-01-07| 0| 0| 0| 6|
| 1|2022-01-08| 0| 0| 0| 6|
| 1|2022-01-09| 1| 1| 1| 7|
| 1|2022-01-10| 2| 1| 1| 8|
| 1|2022-01-11| 3| 1| 1| 9|
| 1|2022-01-12| 0| 0| 0| 9|
| 1|2022-01-13| 0| 0| 0| 9|
| 1|2022-01-14| 1| -1| -1| 8|
| 1|2022-01-15| 2| -1| -1| 7|
| 1|2022-01-16| 3| -1| -1| 6|
| 1|2022-01-17| 4| -1| -1| 5|
| 1|2022-01-18| 5| -1| 0| 5|
| 1|2022-01-19| 6| -1| 0| 5|
| 1|2022-01-20| 0| 0| 0| 5|