value counts of group by in pandas
<p>I am new to pandas and was trying to learn about this feature.</p>

<pre><code>iris = datasets.load_iris()
grps = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
iris = pd.DataFrame(iris.data[:,0],columns = ["SL"])
iris['bins'] = pd.cut(iris["SL"], 10 ,labels = grps )
iris['target'] = datasets.load_iris().target
</code></pre>

<p>I would like to have the number of occurence of each class in each bins. How do i do this as i cannot seem to think of a way. And i would like to plot this output as a stacked histogram</p>

<p>Try this:</p>

<pre><code>In [83]: import seaborn as sns

In [84]: x = iris.groupby(['target','bins']).size().to_frame('occurences').reset_index()

In [85]: x
Out[85]:
    target bins  occurences
0        0    1           9
1        0    2          19
2        0    3          12
3        0    4           9
4        0    5           1
5        1    2           3
6        1    3           2
7        1    4          16
8        1    5          13
9        1    6           7
10       1    7           7
11       1    8           2
12       2    2           1
13       2    4           2
14       2    5           8
15       2    6          13
16       2    7          11
17       2    8           4
18       2    9           5
19       2   10           6

In [86]: sns.barplot(x='bins', y='occurences', hue='target', data=x)
Out[86]: &lt;matplotlib.axes._subplots.AxesSubplot at 0x9b3bb38&gt;
</code></pre>

<p><a href="http://i.stack.imgur.com/qIXbm.png" rel="nofollow"><img src="http://i.stack.imgur.com/qIXbm.png" alt="enter image description here"></a></p>

python pandas matplotlib dataframe