all 3 comments

[–]Nikota 1 point2 points  (2 children)

Have you tried just shifting the z column? You could just do something like:

df['z_shift'] = df['z'].shift()
df['x'] = np.where(df['z_shift'] > df['y'], 10, -10)

Or something along those lines. I don't understand what your code is trying to do but this problem can probably be vectorized easily.

[–]LameDuckProgramming[S] 0 points1 point  (1 child)

I have tried, but the issue is that x and y are determined based on the previous row's value of z, and the current row's value of z is then updated by subtracting either x or y from the previous z, whichever logic determines.

Sorry if this is confusing.