Pandas groupby object filtering
<p>i have a pandas dataframe</p>

<pre><code>df.columns
Index([uâcar_idâ,uâcolorâ,uâmakeâ,uâyearâ)]
</code></pre>

<p>I would like to create a new FILTERABLE object that has the count of each group (color,make,year);</p>

<pre><code>grp = df[[âcolorâ,âmakeâ,âyearâ]].groupby([âcolorâ,âmakeâ,âyearâ]).size()
</code></pre>

<p>which will return something like this</p>

<pre><code>color   make   year     count
black   honda  2011   416
</code></pre>

<p>I would like to be able to filter it, however when I try this:</p>

<pre><code>grp.filter(lambda x: x[âcolorâ]==âblackâ)
</code></pre>

<p>I receive this error</p>

<blockquote>
  <p>TypeError: 'function' object is not iterable</p>
</blockquote>

<p>How do I leverage a 'groupby' object in order to filter the rows out?</p>

<p>I think you need add <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reset_index.html" rel="nofollow"><code>reset_index</code></a> and then output is <code>DataFrame</code>. Last use <a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing" rel="nofollow"><code>boolean indexing</code></a>:</p>

<pre><code>df = df[['color','make','year']].groupby(['color','make','year'])
                                .size()
                                .reset_index(name='count')


df1 = df[df.color == 'black']
</code></pre>

python pandas indexing group-by condition