title: How can I keep all rows of a grouped pandas DataFrage meeting a certain criteria?
body: <p>For a pandas DataFrame with groups I want to keep all rows until the first occurence of a specific value (and discard all other rows).</p>
  
  <p>MWE:</p>
  
  <pre><code>import pandas as pd
  df = pd.DataFrame({'A' : ['foo', 'foo', 'foo', 'bar', 'bar', 'bar', 'tmp'],
                     'B' : [0, 1, 0, 0, 0, 1, 0],
                     'C' : [2.0, 5., 8., 1., 2., 9., 7.]})
  </code></pre>
  
  <p>gives</p>
  
  <pre><code>    A    B  C
  0   foo  0  2.0
  1   foo  1  5.0
  2   foo  0  8.0
  3   bar  0  1.0
  4   bar  0  2.0
  5   bar  1  9.0
  6   tmp  0  7.0
  </code></pre>
  
  <p>and I want to keep all rows for each group (<code>A</code> is the grouping variable) until <code>B == 1</code> (including this row). So, my desired output is</p>
  
  <pre><code>    A    B  C
  0   foo  0  2.0
  1   foo  1  5.0
  3   bar  0  1.0
  4   bar  0  2.0
  5   bar  1  9.0
  6   tmp  0  7.0
  </code></pre>
  
  <p><strong>How can I keep all rows of a grouped DataFrage meeting a certain criteria?</strong></p>
  
  <p>I found <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.groupby.DataFrameGroupBy.filter.html" rel="nofollow noreferrer">how to drop specific groups not meeting a certain criteria (and keeping all other rows of all other groups)</a>, but not how to drop specific rows for all groups. The farest I got was to get the indices of the rows in each group, I want to keep: </p>
  
  <pre><code>df.groupby('A').apply(lambda x: x['B'].cumsum().searchsorted(1))
  </code></pre>
  
  <p>resulting in</p>
  
  <pre><code>A
  bar    2
  foo    1
  tmp    1
  </code></pre>
  
  <p>Which isn't sufficient, as it does not return the actual data (and it might be better, if for <code>tmp</code> the result was <code>0</code>)</p>