This might be done with a rolling function in pandas probably, not sure but I would like to apply the following function for a list, the current state S in position x is defined as
S[x] = if S[x-1] > 0 S[x-1] -1 + S[x] else S[x] -1 for x > 1 It can be understood as the current state -1 and the current state... This is because I need to do a kind of cumulative sum of all the previous positions -1 + the current position.
An example for the list
[1,1,2,0,0,2]returns this values
[0,0,1,0,-1,1]because:
S[0] = 1 - 1 = 0S[1] = S[1] - 1 + S[0] = 1 - 1 + 0 = 0S[2] = S[2] - 1 + S[1] = 2 - 1 + 0 = 1S[3] = S[3] - 1 + S[2] = 0 - 1 + 1 = 0S[4] = S[4] - 1 + S[3] = 0 - 1 + 0 = -1S[5] = S[5] - 1 (no S[4] because the else rule being smaller than 0) = 2 - 1 = 1 I am pretty sure this can probably be done in pandas but I am also open to a standard python function I send a list to (prefer pandas though).
Have been trying recursion and failed miserably.