title: How to filter Pandas rows based on last/next row?
body: <p>I have two data sets from different pulse oximeters, and plot them with pyplot as displayed below. As you may see, the green data sheet has alot of outliers(vertical drops). In my work I've defined these outlayers as non-valid in for my statistical analysis, they are must certainly not measurements. Therefore I argue that I can simply remove them. </p>
  
  <p>The characteristics of these rogue values is that they're single(or top two) value outliers(see df below). The "real" sample values are either the same as the previous value, or +-1. In e.g. java(pseudo code) I would do something like:</p>
  
  <pre><code>for(i; i &lt;df.length; i++)
    if (df[i+1|-1].spo2 - df[i].spo2 &gt; 1|-1)
      df[i].drop
  </code></pre>
  
  <p>What would be the pandas(numpy?) equivalent of what I'm trying to do, remove values that is more/less than 1 compared to the last/next value?</p>
  
  <hr>
  
  <p>df:</p>
  
  <pre><code>    time, spo2
  1900-01-01 18:18:41.194  98.0
  1900-01-01 18:18:41.376  98.0
  1900-01-01 18:18:41.559  78.0
  1900-01-01 18:18:41.741  98.0
  1900-01-01 18:18:41.923  98.0
  1900-01-01 18:18:42.105  90.0
  1900-01-01 18:18:42.288  97.0
  1900-01-01 18:18:42.470  97.0
  1900-01-01 18:18:42.652  98.0
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/0pNIC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0pNIC.png" alt="sensor data from two pulse oximeters"></a> </p>