Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count the number of zeros between two non zero numbers sequentially in a dataframe

Tags:

python

pandas

I have a pandas dataframe and i am trying to count the number of zeros before every digit till a non zero number shows up and capture it into the next column. How can I do this using pandas?

This is how the output needs to be in zeroCumulative column. For example, number of zeros before 101 is 0, number of zeros before 73 is 3 and so on. Number of zeros before any zero also needs to be counted.

value zeroCumulative 
70
127         0 
101         0 
0           0 
0           1 
0           2 
73          3 
0           0 
55          1 
0           0 

Thanks in advance!

like image 722
Rishabh Malhotra Avatar asked Dec 19 '25 07:12

Rishabh Malhotra


1 Answers

To improve performance it is possible to use a vectorized solution, this is similar to this solution with Series.shift of column and compare by 0:

a = df['value'].shift().eq(0)
b = a.cumsum()
df['new'] = b.sub(b.mask(a).ffill().fillna(0)).astype(int)
print (df)
   value  zeroCumulative  new
0     70               0    0
1    127               0    0
2    101               0    0
3      0               0    0
4      0               1    1
5      0               2    2
6     73               3    3
7      0               0    0
8     55               1    1
9      0               0    0
like image 81
jezrael Avatar answered Dec 20 '25 19:12

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!