title: panda dataframe row wise iteration with referencing previous row values for conditional matching
body: <p>I have to find out how many times a bike was on overspeed, and in each instances for how long(for simplicity how many kms)</p>
  
  <pre><code>df = pd.DataFrame({'bike':['b1']*15, 'km':list(range(1,16)), 'speed':[20,30,38,33,28,39,26,33,35,46,53,27,37,42,20]})
  &gt;&gt;&gt; df
     bike  km  speed
  0    b1   1     20
  1    b1   2     30
  2    b1   3     38
  3    b1   4     33
  4    b1   5     28
  5    b1   6     39
  6    b1   7     26
  7    b1   8     33
  8    b1   9     35
  9    b1  10     46
  10   b1  11     53
  11   b1  12     27
  12   b1  13     37
  13   b1  14     42
  14   b1  15     20
  #Expected result is
  bike  last_OS_loc  for_how_long_on_OS
  b1      4                2km
  b1      11               5km
  b1      15               1km
  </code></pre>
  
  <p>Now Logic-   </p>
  
  <ol>
  <li><p>has to flag the speed >= 30 as Overspeed_Flag</p></li>
  <li><p>If the speed remains on more than 30 for 1 or 1+km then those continuation are treated as a overspeed session (eg: when b1 was in between 2 to 4 km, 6to11, 13-14km, MARK it was not a overspeed session when b1 was at 6km, as it was only for that row, no continuation on >30 found). </p></li>
  <li><p>then measure for a session, how long/for how many kms he remains at overspeed limit. Refer expected result tab.</p></li>
  <li><p>also finding out for a overspeed session what was the last km mark.</p></li>
  </ol>
  
  <p>Kindly suggest how can i achieve this. And do let me know if anything is not clear in the question.</p>
  
  <p>P:S: i am also trying, but it is little complex for me(Pretty confused on how to mark if it is a continuation of OS_flag or a single instance of OS.), Will get back if successful in doing this. Thanks in ADV.</p>