title: Subset two consecutive event occurrence in pandas
body: <p>I'm trying to get a subset of my data whenever there is consecutive occurrence of an two events in that order. The event is time-stamped. So every time there are continuous 2's and then continuous 3's, I want to subset that to a dataframe and append it to a dictionary. The following code does that but I have to apply this to a very large dataframe of more than 20 mil obs. This is extremely slow using iterrows. How can I make this fast?</p>
  <pre><code>df = pd.DataFrame({'Date': [101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122],
          'Event': [1,1,2,2,2,3,3,1,3,2,2,3,1,2,3,2,3,2,2,3,3,3]})
  
  dfb = pd.DataFrame(columns = df.columns)
  C = {}
  f1 = 0
  
  for index, row in df.iterrows():
      if ((row['Event'] == 2) &amp; (3 not in dfb['Event'].values)):
          dfb = dfb.append(row)
          f1 =1
      elif ((row['Event'] == 3) &amp; (f1 == 1)):
          dfb = dfb.append(row)
      elif 3 in dfb['Event'].values:
          f1 = 0
          C[str(dfb.iloc[0,0])] = dfb
          del dfb
          dfb = pd.DataFrame(columns = df.columns)
          if row['Event'] == 2:
              dfb = dfb.append(row)
              f1 =1
      else:
          f1=0
          del dfb
          dfb = pd.DataFrame(columns = df.columns)
  
  </code></pre>
  <p>Edit: The desired output is basically a dictionary of the subsets shown in the image<a href="https://i.stack.imgur.com/ClWZs.png" rel="nofollow noreferrer">https://i.stack.imgur.com/ClWZs.png</a></p>