Reputation: 515
I have Pandas series containing negative values and zeros.
VALUES COUNTER
0 0.000000 0
1 -0.058552 1
2 -0.034268 2
3 0.000000 0
4 -0.007787 1
5 -0.002754 2
6 -0.000353 3
7 -0.004303 4
8 -0.016923 5
9 0.000000 0
I would like to create column counter that will start counting strikes of negative values and reset when there will be zero.
Any tips how to effectively do this? I have seen lots of similar questions on cumulative sum using groups, but I cannot find solution for this one.
Upvotes: 1
Views: 1346
Reputation:
df.groupby(df['VALUES'].eq(0).cumsum()).cumcount()
Output:
>>> df
VALUES COUNTER
0 0.000000 0
1 -0.058552 1
2 -0.034268 2
3 0.000000 0
4 -0.007787 1
5 -0.002754 2
6 -0.000353 3
7 -0.004303 4
8 -0.016923 5
9 0.000000 0
Upvotes: 3