3

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!

petezurich
  • 9,280
  • 9
  • 43
  • 57

2 Answers2

3

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
petezurich
  • 9,280
  • 9
  • 43
  • 57
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

Try using cumsum() and cumcount():

df['zeroCumulative'] = df["value"].groupby((df["value"].ne(0)).shift().cumsum()).cumcount()

df:

    value   zeroCumulative
0   70      0
1   127     0
2   101     0
3   0       0
4   0       1
5   0       2
6   73      3
7   0       0
8   55      1
9   0       0
Pygirl
  • 12,969
  • 5
  • 30
  • 43