title: Cumulative count reset on condition
body: <p>I have a dataframe similar to this:</p>
  <pre><code>df = pd.DataFrame({'col1': ['a', 'a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'c'],
                   'col2': [1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, 2, 2],
                   'col3': [1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0],
                   'desired': [0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1]})
  </code></pre>
  <p>I want to apply a rolling sum on <code>col3</code> which resets when either of <code>col1</code> or <code>col2</code> changes, or when the previous value of <code>col3</code> was zero.</p>
  <p>Note that the count is offset by 1 cell. This means the desired value for a new <code>(col1, col2)</code> combination will always be zero.</p>
  <p>The code below demonstrates the required logic. However, it takes nearly 4 minutes on the dataset below.</p>
  <pre><code>des = []
  count = 0
  for i in range(1, len(df)):
      des.append(count)
      if (df.iloc[i-1].col1 == df.iloc[i].col1) &amp; \
         (df.iloc[i-1].col2 == df.iloc[i].col2) &amp; \
         (df.iloc[i-1].col3 == 1):
      
          count += 1
      else:
          count = 0
      
  des.append(0)
  
  df['desired'] = des
  </code></pre>
  <p>A bigger dataset to test on: <a href="https://www.dropbox.com/s/hbafcq6hdkh4r9r/test.csv?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/hbafcq6hdkh4r9r/test.csv?dl=0</a></p>