title: Problems while trying to select rows based on a restriction?
body: <p>Given the below dataframe:</p>
  <pre><code>Col A           Col B 
  orange          yes         
  apple           no
  watermelon      yes
  peas            yes
  carrot          yes
  carrot          no
  apple           no
  orange          yes         
  watermelon      no
  carrot          yes
  apple           no
  peas            no
  </code></pre>
  <p>How can I select 4 different elements from <code>Col A</code> with the restriction that <code>Col B</code> must have 2 <code>True</code> and 2 <code>False</code> interleaved values, for example, a valid extraction looks like this:</p>
  <pre><code>Col A           Col B 
  orange          yes         
  apple           no
  carrot          yes
  watermelon      no
  </code></pre>
  <p>I tried to use the query API call:</p>
  <pre><code>a = a_df.query(&quot;Col B == True&quot;)[:4]
  b = a_df.query(&quot;Col B == False&quot;)[:4]
  
  merged = pd.concat([a, b], axis=0).drop_duplicates(subset=['Col B'])
  </code></pre>
  <p>However, I am only getting two values instead of the requiered four. Any idea of how to get the above filtering with pandas?</p>
  <p>From @sammywemmy answer I tried:</p>
  <pre><code>df['X'] = np.where(df['Col B']=='yes', True, False)
  forward = df['X'].gt(df['X'].shift(-1))
  backward = df['X'].lt(df['X'].shift(-1))
  filters = df.index[forward].union(df.index[backward])
  
  
  df.loc[filters].drop_duplicates(subset=['Col B'])
  </code></pre>
  <p>However, it doesnt work because I only get a dtaframe with two rows.</p>