title: How to elegantly remove values in a column based on 5 day/idx rule?
body: <p>I have a dataframe like as given below</p>
  
  <pre><code>test1 = pd.DataFrame({
       'subject_id':[1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2],
       'flag' : ['','','T1','T1','T1','T1','T1','T1','T1','T1','','','T1','T1','T1','T1','T1','T1','T1','T1']
   })
  </code></pre>
  
  <p>It looks like as shown below</p>
  
  <p><a href="https://i.stack.imgur.com/HtA4m.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HtA4m.png" alt="enter image description here"></a></p>
  
  <p>As per the rule/logic, <code>T1</code> can appear in flag field only after <code>5</code> days/records from it's first occurrence. For example if <code>T1</code> had occurred on <code>3rd</code> index, it can then only occur on <code>9th</code> index and more..Anything before that are invalid and has to be removed.</p>
  
  <p>I tried the below. Though this works, it doesn't look elegant and not suitable for all subjects. </p>
  
  <pre><code>a = test1[test1['flag']=='T1'].index.min()
  test1.loc[a+1:a+6, 'flag'] = ''
  </code></pre>
  
  <p>How can I do this check indvidually for all the subjects? Each subject and its flag should follow this rule</p>
  
  <p>I expect my output to be like as shown below. You can see the invalid flags are removed</p>
  
  <p><a href="https://i.stack.imgur.com/Twmzg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Twmzg.png" alt="enter image description here"></a></p>