title: pandas filtering rows by group value
body: <p>Here is the data that I'm practicing with</p>
  
  <pre><code>import pandas as pd
  df = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv")
  </code></pre>
  
  <p>I want to filter individual rows by a grouped valued. I know I can do the following to filter groups</p>
  
  <pre><code>df.groupby("day").filter(lambda x: x['total_bill'].mean() &gt; 20).day.unique()
  </code></pre>
  
  <p>Which finds which days have an average bill greater than $20. This works because groupby.filter takes a function to apply to each subframe that should return True or False. However, what if I want to find every meal (row) whose value for total_bill is greater than that day's total_bill. Eg if a row has <code>total_bill</code> of <code>22</code> and was on Sunday, then it should be kept because Sunday's <code>total_bill</code> average was <code>21.41</code>.</p>
  
  <p>This is my attempt:</p>
  
  <pre><code>df.groupby('day').apply(lambda x: x['total_bill'] &gt; x['total_bill'].mean())
  </code></pre>
  
  <p>However, this yields something that looks like this (first few rows)</p>
  
  <pre><code>day    
  Fri  90     True
       91     True
       92    False
       93    False
       94     True
  Name: total_bill, dtype: bool
  </code></pre>
  
  <p>This is not in the same order as the dataframe so I can't just take the boolean column and use it to index the data.</p>
  
  <p>So now I do the following:</p>
  
  <pre><code>grouped = (df
             .groupby('day')
             .apply(lambda x: x['total_bill'] &gt; x['total_bill'].mean())
             .reset_index())
  
  index_bill = (grouped
               .loc[grouped.total_bill == True, 'level_1'].values)
  df.loc[index_bill]
  </code></pre>
  
  <p>Which gives me the desired result... there has to be an easier way, right? Please let me know if there is a proper way to do this. If not, is there at least a way I can combine the two steps into one? I can do the groupby, but am not sure how to get the values without storing the grouped object as a variable and then referencing it. Thanks!</p>