title: Compare preceding two rows with subsequent two rows of each group till last record
body: <p>I had a question earlier which is deleted and now modified to a less verbose form for you to read easily.</p>
  
  <p>I have a dataframe as given below</p>
  
  <pre><code>df = pd.DataFrame({'subject_id' :[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2],'day':[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] , 'PEEP' :[7,5,10,10,11,11,14,14,17,17,21,21,23,23,25,25,22,20,26,26,5,7,8,8,9,9,13,13,15,15,12,12,15,15,19,19,19,22,22,15]})
  df['fake_flag'] = ''
  </code></pre>
  
  <p>I would like to fill values in column <code>fake_flag</code> based on the below rules</p>
  
  <p>1) if preceding two rows are constant (ex:5,5) or decreasing (7,5), then pick the highest of the two rows. In this case, it is 7 from (7,5) and 5 from (5,5)</p>
  
  <p>2) Check whether the current row is greater than the output from rule 1 by 3 or more points (>=3) and it repeats in another (next) row (2 occurrences of same value). It can be 8/gt 8(if rule 1 output is 5). ex: (8 in row <code>n</code>,8 in row <code>n+1</code> or 10 in row <code>n</code>,10 in row <code>n+1</code>) If yes, then key in <code>fake VAC</code> in the <code>fake_flag column</code></p>
  
  <p>This is what I tried</p>
  
  <pre><code>for i in t1.index:
  if i &gt;=2:
      print("current value is  ", t1[i])
      print("preceding 1st (n-1) ", t1[i-1])
      print("preceding 2nd (n-2) ", t1[i-2])
      if (t1[i-1] == t1[i-2] or t1[i-2] &gt;= t1[i-1]): # rule 1 check
          r1_output = t1[i-2] # we get the max of these two values (t1[i-2]), it doesn't matter when it's constant(t1[i-2] or t1[i-1]) will have the same value anyway
          print("rule 1 output is ", r1_output)
          if t1[i] &gt;= r1_output + 3:
              print("found a value for rule 2", t1[i])
              print("check for next value is same as current value", t1[i+1])
              if (t1[i]==t1[i+1]): # rule 2 check
                  print("fake flag is being set")
                  df['fake_flag'][i] = 'fake_vac'
  </code></pre>
  
  <p>This check should happen for all records (one by one) for each subject_id. I have a dataset which has million records. Any efficient and elegant solution is helpful. I can't run a loop over million records.</p>
  
  <p>I expect my output to be like as shown below</p>
  
  <p><strong>subject_id = 1</strong></p>
  
  <p><a href="https://i.stack.imgur.com/tkSJu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tkSJu.png" alt="enter image description here"></a></p>
  
  <p><strong>subject_id = 2</strong></p>
  
  <p><a href="https://i.stack.imgur.com/vHYhK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vHYhK.png" alt="enter image description here"></a></p>