I have a numpy array with only -1, 1 and 0, like this:
np.array([1,1,-1,-1,0,-1,1])
I would like a new array that counts the -1 encountered. The counter must reset when a 0 appears and remain the same when it's a 1:
Desired output:
np.array([0,0,1,2,0,1,1])
The solution must be very little time consuming when used with larger array (up to 100 000)
Edit: Thanks for your contribution, I've a working solution for now.
I'm still looking for a non-iterative way to solve it (no for
loop). Maybe with a pandas Series and the cumsum()
method ?