3

Currently I have a dataset below and I try to accumulate the value if ColA is 0 while reset the value to 0 (restart counting again) if the ColA is 1 again.

ColA  
 1        
 0        
 1        
 1        
 0        
 1        
 0         
 0        
 0        
 1        
 0        
 0        
 0        

My expected result is as below.

ColA  Accumulate
 1        0
 0        1
 1        0 
 1        0 
 0        1
 1        0
 0        1 
 0        2 
 0        3
 1        0
 0        1
 0        2
 0        3

The current code I use

test['Value'] = np.where ( (test['ColA']==1),test['ColA'].cumsum() ,0)


ColA   Value
 1        0
 0        1
 1        0 
 1        0 
 0        2
 1        0
 0        3 
 0        4 
 0        5
 1        0
 0        6
 0        7
 0        8
Patrick Yoder
  • 1,065
  • 4
  • 14
  • 19
superhi
  • 33
  • 2

2 Answers2

3

Use cumsum if performance is important:

a = df['ColA'] == 0
cumsumed = a.cumsum()
df['Accumulate'] = cumsumed-cumsumed.where(~a).ffill().fillna(0).astype(int)

print (df)
    ColA  Accumulate
0      1           0
1      0           1
2      1           0
3      1           0
4      0           1
5      1           0
6      0           1
7      0           2
8      0           3
9      1           0
10     0           1
11     0           2
12     0           3
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

This should do it:

test['Value'] = (test['ColA']==0) * 1 * (test['ColA'].groupby((test['ColA'] != test['ColA'].shift()).cumsum()).cumcount() + 1)

It is an adaption of this answer.

zipa
  • 27,316
  • 6
  • 40
  • 58