title: How to count distinct values in a combination of columns while grouping by in pandas?
body: <p>I have a pandas data frame. I want to group it by using one combination of columns and count distinct values of another combination of columns.</p>
  
  <p>For example I have the following data frame:</p>
  
  <pre><code>   a   b    c     d      e
  0  1  10  100  1000  10000
  1  1  10  100  1000  20000
  2  1  20  100  1000  20000
  3  1  20  100  2000  20000
  </code></pre>
  
  <p>I can group it by columns <code>a</code> and <code>b</code> and count <strong>distinct</strong> values in the column <code>d</code>:</p>
  
  <pre><code>df.groupby(['a','b'])['d'].nunique().reset_index()
  </code></pre>
  
  <p>As a result I get:</p>
  
  <pre><code>   a   b  d
  0  1  10  1
  1  1  20  2
  </code></pre>
  
  <p>However, I would like to count distinct values in a <strong>combination of columns</strong>. For example if I use <code>c</code> and <code>d</code>, then in the first group I have only one unique combination (<code>(100, 1000)</code>) while in the second group I have two distinct combinations: <code>(100, 1000)</code> and <code>(100, 2000)</code>.</p>
  
  <p>The following naive "generalization" does not work:</p>
  
  <pre><code>df.groupby(['a','b'])[['c','d']].nunique().reset_index()
  </code></pre>
  
  <p>because <code>nunique()</code> is not applicable to data frames.</p>