title: Count the number of consecutive items in dataframe
body: <p>Suppose I have a dataset like the following:</p>
  
  <pre><code>import pandas as pd
  d1={'Participant_ID':['P1','P1','P1','P1','P1','P1','P2','P2','P2','P2','P2','P2'],
  'Brand_Type':['B1','B1','B2','B1','B2','B2','B2','B2','B1','B2','B1','B1'],
  'Date':['5/6/2010','1/27/2010','4/3/2010','7/11/2010','3/1/2010','6/8/2010',
         '5/6/2010','1/27/2010','4/3/2010','7/11/2010','3/1/2010','6/8/2010']}
  d11=pd.DataFrame(d1)
  d111=d11.sort_values(by=['Participant_ID','Date'])
  </code></pre>
  
  <p>Suppose each participant in the study can consume one of two brands (B1 or B2). I want to create a variable to count how many times in the past they consumed one of the two brands. </p>
  
  <p>This is the output that I want:
  <a href="https://i.stack.imgur.com/2qu1I.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2qu1I.png" alt="enter image description here"></a></p>
  
  <p>The logic is as follows:</p>
  
  <p>i) For row 1, since there is no prior data, start with 0 in both columns.</p>
  
  <p>ii) For row 2, since the participant consumed B1 in the prior period, <strong>B1_consecutive is 1</strong>, B2_consecutive is zero.</p>
  
  <p>iii) For row 3, since the participant consumed B2 in the prior period, B1_consecutive is 0, <strong>B2_consecutive 1</strong>.</p>
  
  <p>iv) For row 4, since the participant consumed B2 in the prior period, B1_consecutive is 0, <strong>B2_consecutive 2</strong>.</p>