COUNT DISTINCT / nunique within groups
<p>I want to count the number of distinct tuples within each group:</p>

<pre><code>df = pd.DataFrame({'a': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
              'b': [1, 2, 1, 2, 1, 2, 1, 2],
              'c': [1, 1, 2, 2, 2, 1, 2, 1]})
counts = count_distinct(df, by='a', columns=['b', 'c'])
assert counts == pd.Series([4, 2], index=['A', 'B'])
</code></pre>

<p>In other words, counts should report that for group 'A', there are four distinct tuples and for group 'B', there are two.</p>

<p>I tried using <code>df.groupby('a')['b', 'c'].nunique()</code>, but <code>nunique</code> works only with a single column.</p>

<p>I know I could count distinct tuples by df.groupby(['b', 'c']), but that means I have use a slow <code>apply</code> with a pure python function (the number of groups of column 'a' is large).</p>

<p>I could convert the 'b' and 'c' columns into a single column of tuples, but that would be super slow since it will no longer use vectorized operations.</p>

<p>I think your logic is equivalent to count the size of data frames grouped by column <code>a</code> after dropping the duplicated values of combined columns <code>a</code>, <code>b</code> and <code>c</code>, since duplicated tuples within each group must also be duplicated records in the data frame assuming your data frame contains only columns <code>a</code>, <code>b</code> and <code>c</code> and vice versa:</p>

<pre><code>df.drop_duplicates().groupby('a').size()

# a
# A    4
# B    2
# dtype: int64
</code></pre>

python python-3.x pandas