title: Iterating a Pandas dataframe over 'n' next rows
body: <p>I have this Pandas dataframe <code>df</code>:</p>
  
  <pre><code>station a_d direction
     a     0      0
     a     0      0
     a     1      0
     a     0      0
     a     1      0
     b     0      0
     b     1      0
     c     0      0
     c     1      0
     c     0      1
     c     1      1
     b     0      1
     b     1      1
     b     0      1
     b     1      1
     a     0      1
     a     1      1
     a     0      0
     a     1      0
  </code></pre>
  
  <p>I'd assign a value_id that increments when direction value change and refers only to the last pair of station value first it changes with different [0,1] a_d value. I can ignore the last (in this example the last two) dataframe row. In other words:</p>
  
  <pre><code>station a_d direction id_value
     a     0      0
     a     0      0
     a     1      0
     a     0      0        0
     a     1      0        0
     b     0      0        0
     b     1      0        0
     c     0      0        0
     c     1      0        0
     c     0      1        1
     c     1      1        1
     b     0      1         
     b     1      1        
     b     0      1        1
     b     1      1        1
     a     0      1        1
     a     1      1        1
     a     0      0
     a     1      0
  </code></pre>
  
  <p>Using <code>df.iterrows()</code> i write this script:</p>
  
  <pre><code>df['value_id'] = ""
  value_id = 0
  row_iterator = df.iterrows()
  for i, row in row_iterator:
      if i == 0:
          continue
      elif (df.loc[i-1,'direction'] != df.loc [i,'direction']):
          value_id += 1
      for z in range(1,11):
          if i+z &gt;= len(df)-1:
              break
          elif (df.loc[i+1,'a_d'] == df.loc [i,'a_d']):
              break
          elif (df.loc[i+1,'a_d'] != df.loc [i,'a_d']) and (df.loc [i+2,'station'] == df.loc [i,'station'] and (df.loc [i+2,'direction'] == df.loc [i,'direction'])):
              break
          else:
              df.loc[i,'value_id'] = value_id
  </code></pre>
  
  <p>It works but it's very slow. With a <code>10*10^6</code> rows dataframe I need a faster way. Any idea?</p>
  
  <p>@user5402 code works well but I note that a <code>break</code> after the last <code>else</code> reduce computational time also:</p>
  
  <pre><code>df['value_id'] = ""
  value_id = 0
  row_iterator = df.iterrows()
  for i, row in row_iterator:
      if i == 0:
          continue
      elif (df.loc[i-1,'direction'] != df.loc [i,'direction']):
          value_id += 1
      for z in range(1,11):
          if i+z &gt;= len(df)-1:
              break
          elif (df.loc[i+1,'a_d'] == df.loc [i,'a_d']):
              break
          elif (df.loc[i+1,'a_d'] != df.loc [i,'a_d']) and (df.loc [i+2,'station'] == df.loc [i,'station'] and (df.loc [i+2,'direction'] == df.loc [i,'direction'])):
              break
          else:
              df.loc[i,'value_id'] = value_id
              break
  </code></pre>