title: Groupby value counts on the dataframe pandas
body: <p>I have the following dataframe:</p>
  
  <pre><code>df = pd.DataFrame([
      (1, 1, 'term1'),
      (1, 2, 'term2'),
      (1, 1, 'term1'),
      (1, 1, 'term2'),
      (2, 2, 'term3'),
      (2, 3, 'term1'),
      (2, 2, 'term1')
  ], columns=['id', 'group', 'term'])
  </code></pre>
  
  <p>I want to group it by <code>id</code> and <code>group</code> and calculate the number of each term for this id, group pair.</p>
  
  <p>So in the end I am going to get something like this:</p>
  
  <p><a href="https://i.stack.imgur.com/DIwPYm.png" rel="noreferrer"><img src="https://i.stack.imgur.com/DIwPYm.png" alt="enter image description here"></a></p>
  
  <p>I was able to achieve what I want by looping over all the rows with <code>df.iterrows()</code> and creating a new dataframe, but this is clearly inefficient. (If it helps, I know the list of all terms beforehand and there are ~10 of them).</p>
  
  <p>It looks like I have to group by and then count values, so I tried that with <code>df.groupby(['id', 'group']).value_counts()</code> which does not work because <a href="http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.core.groupby.SeriesGroupBy.value_counts.html" rel="noreferrer">value_counts</a> operates on the groupby series and not a dataframe.</p>
  
  <p>Anyway I can achieve this without looping?</p>