How can I implement something similar to `value_counts()` in a groupby?
<p>Suppose I had the following data</p>

<pre><code>Orchard    Tree
1           Apple
2           Peach
1           Peach
3           Apple
</code></pre>

<p>How could I group by <code>orchard</code> and show how many times each tree occurs in the orchard?  The result would look like</p>

<pre><code>Tree     Apple  Peach
Orchard
1          1     1
2          0     1
3          1     0
</code></pre>

<p>Is <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.pivot_table.html" rel="nofollow">pivot_table()</a> what you want?</p>

<pre><code>In [48]: df
Out[48]:
   Orchard   Tree
0        1  Apple
1        2  Peach
2        1  Peach
3        3  Apple

In [49]: df.pivot_table(index='Orchard', columns='Tree', aggfunc='size', fill_value=0)
Out[49]:
Tree     Apple  Peach
Orchard
1            1      1
2            0      1
3            1      0
</code></pre>

<p>or using <code>groupby()</code> and <code>unstack()</code> (that's how <code>pivot_table()</code> does it under the hood):</p>

<pre><code>In [57]: df.groupby(['Orchard','Tree']).size().unstack('Tree', fill_value=0)
Out[57]:
Tree     Apple  Peach
Orchard
1            1      1
2            0      1
3            1      0
</code></pre>

python pandas dataframe