The dataframe is already sorted out by date,
col1 ==1 value is unique,
and only the 0 have duplicates.
I have a dataframe looks like this call it df
+--------+----+----+
date |col1|col2|
+--------+----+----+
2020-08-01| 5| -1|
2020-08-02| 4| -1|
2020-08-03| 3| 3|
2020-08-04| 2| 2|
2020-08-05| 1| 4|
2020-08-06| 0| 1|
2020-08-07| 0| 2|
2020-08-08| 0| 3|
2020-08-09| 0| -1|
+--------+----+----+
The condition is when col1 == 1, then we start adding backwards from col2 ==4, (eg. 4,5,6,7,8,...) and the after col2 == 4 return 0 all the way (eg. 4,0,0,0,0...)
So, my resulted df will look something like this.
+--------+----+----+----+
date |col1|col2|want
+--------+----+----+----+
2020-08-01| 5| -1| 8 |
2020-08-02| 4| -1| 7 |
2020-08-03| 3| 3| 6 |
2020-08-04| 2| 2| 5 |
2020-08-05| 1| 4| 4 |
2020-08-06| 0| 1| 0 |
2020-08-07| 0| 2| 0 |
2020-08-08| 0| 3| 0 |
2020-08-09| 0| -1| 0 |
+---------+----+----+----+
Enhancement: I want to add additional condition where col2 == -1 when col1 == 1 row, and -1 goes consecutive, then I want to count consecutive -1, and then add with next col2 == ? value. so here's an example to clear.
+--------+----+----+----+
date |col1|col2|want
+--------+----+----+----+
2020-08-01| 5| -1| 11|
2020-08-02| 4| -1| 10|
2020-08-03| 3| 3| 9 |
2020-08-04| 2| 2| 8 |
2020-08-05| 1| -1| 7 |
2020-08-06| 0| -1| 0 |
2020-08-07| 0| -1| 0 |
2020-08-08| 0| 4| 0 |
2020-08-09| 0| -1| 0 |
+---------+----+----+----+
so, we see 3 consecutive -1s, (we only care about first consecutive -1s) and after the consecutive we have 4, then we would have 4+ 3 =7 at the col1 ==1 row. is it possible?
col1has multiple1values? I can already see duplicates for0, can duplicates exist for other values as well? What is the correct sorting in that case? - Dusan Vasiljevic