title: Python Pandas DataFrame delete rows based on values in multiple previous rows
body: <p>I have a dataframe with dates as index and one column which is instructions to enter and exit trades.  Each row is one of </p>
  
  <pre><code>'short_entry', 'short_exit', 'long_entry', 'long_exit'.
  </code></pre>
  
  <p>Rules:</p>
  
  <p>1 - You cannot exit a short (short_exit) position if you don't already hold a short position (short_entry). Likewise for long positions.</p>
  
  <p>2 - You can only enter another short posn, if the previous short_entry has been closed with a corresponding short_exit.  Likewise with long entry and exits.</p>
  
  <p><a href="https://i.stack.imgur.com/FZ7uS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FZ7uS.png" alt="enter image description here"></a></p>
  
  <p>Based on the rules the first four rows would be deleted and the first trade entered would be on 2008-02-28 followed by short_exit on 2008-03-27.  The rest of the df would be updated accordingly.</p>
  
  <p>I have read pretty much everything I can find in pandas docs and online helps.  There are answers to delete rows based on values on a single row above (use .shift()), or use if-statements inside .loc().  But I just cannot get my head around how to put all these together to delete a row based on values of multiple previous rows.  I can do it easily using for loops and df.itertuples().</p>
  
  <p>Is there a pandas pythonic way of doing this?  Any help and hints would be greatly appreciated.</p>
  
  <p>Thanks</p>