title: How to use the condition with other rows (previous moments in time series data), in pandas, python3
body: <p>I have a pandas.dataframe <code>df</code>. It is a time series data, with 1000 rows and 3 columns. What I want is given in the pseudo-code below.</p>
  
  <pre><code>for each row
      if the value in column 'colA' at [this_row-1] is higher than 
      the value in column 'B' at [this_row-2] for more than 3%
          then set the value in 'colCheck' at [this_row] as True.
  
  Finally, pickout all the rows in the df where 'colCheck' are True.
  </code></pre>
  
  <p>I will use the following example to further demonstrate my purpose.</p>
  
  <pre><code> df =     
               'colA', 'colB', 'colCheck'
   Dates
   2017-01-01,     20,     30,      NAN
   2017-01-02,     10,     40,      NAN
   2017-01-03,     50,     20,     False
   2017-01-04,     40,     10,      True
  </code></pre>
  
  <p>First, when <code>this_row = 2</code> (the 3rd row, where the date is 2017-01-03), the value in <code>colA</code> at [this_row-1] is <code>10</code>, the value in <code>colB</code> at [this_row-2] is <code>30</code>. So <code>(10-30)/30 = -67% &lt; 3%</code>, so the value in <code>colCheck</code> at [this_row] is False. </p>
  
  <p>Likewise, when <code>this_row = 3</code>,  <code>(50-40)/40 = 25% &gt; 3%</code>, so the value in <code>colCheck</code> at [this_row] is True.</p>
  
  <p>Last but not least, the first two rows in <code>colCheck</code> should be NAN, since the calculation needs to access [this_row-2] in <code>colB</code>. But the first two rows do not have [this_row-2].</p>
  
  <p>Besides, the criteria of <code>3%</code> and [row-1] in <code>colA</code>, [row-2] in <code>colB</code> are just examples. In my real project, they are situational, e.g. <code>4%</code> and [row-3].</p>
  
  <p>I am looking for concise and elegant approach. I am using Python3.</p>
  
  <p>Thanks.</p>