<h4 dir="auto">Currently, pd.DataFrame does not have a <code class="notranslate">.nunique</code> method</h4>
<p dir="auto"><code class="notranslate">DataFrames</code> have various aggregation methods that they share with <code class="notranslate">series</code>, examples being  <code class="notranslate">.sum</code>, <code class="notranslate">.count</code> etc. Likewise with <code class="notranslate">DataFrame.groupby</code>-objects.</p>
<p dir="auto">In many cases, a <code class="notranslate">.nunique</code> would be useful as on <code class="notranslate">DataFrame</code> and <code class="notranslate">DataFrame.groupby</code>-objects, for example when you're trying to assess the diversity of data in a DataFrame.</p>
<h4 dir="auto">Expected Output</h4>
<p dir="auto">When you're using <code class="notranslate">DataFrame.nunique</code>, with <code class="notranslate">n</code> columns,  you should get back a <code class="notranslate">series</code> where the index values are the dataframe.columns values, and the series values are the count of unique values, for each column.</p>
<p dir="auto">When exploring a data set, I often need to <code class="notranslate">df.apply(pd.Series.nunique)</code> or <code class="notranslate">df.apply(lambda x: x.nunique())</code>. How about adding this as <code class="notranslate">nunique()</code>-method parallel to <code class="notranslate">DataFrame.count()</code> (<code class="notranslate">count</code> and <code class="notranslate">unique</code> are also the two most basic infos displayed by <code class="notranslate">DataFrame.describe()</code>)?</p>
<p dir="auto">I think there are also use cases for this as a <code class="notranslate">groupby</code>-method, for example when checking a candidate primary key for different lines (values):</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; import pandas as pd
&gt;&gt;&gt; df = pd.DataFrame({'id': ['spam', 'eggs', 'eggs', 'spam'], 'value': [1, 5, 5, 2]})
&gt;&gt;&gt; df.groupby('id').filter(lambda g: (g.apply(pd.Series.nunique) &gt; 1).any())
     id  value
0  spam      1
3  spam      2"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">df</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>({<span class="pl-s">'id'</span>: [<span class="pl-s">'spam'</span>, <span class="pl-s">'eggs'</span>, <span class="pl-s">'eggs'</span>, <span class="pl-s">'spam'</span>], <span class="pl-s">'value'</span>: [<span class="pl-c1">1</span>, <span class="pl-c1">5</span>, <span class="pl-c1">5</span>, <span class="pl-c1">2</span>]})
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">df</span>.<span class="pl-en">groupby</span>(<span class="pl-s">'id'</span>).<span class="pl-en">filter</span>(<span class="pl-k">lambda</span> <span class="pl-s1">g</span>: (<span class="pl-s1">g</span>.<span class="pl-en">apply</span>(<span class="pl-s1">pd</span>.<span class="pl-v">Series</span>.<span class="pl-s1">nunique</span>) <span class="pl-c1">&gt;</span> <span class="pl-c1">1</span>).<span class="pl-en">any</span>())
     <span class="pl-s1">id</span>  <span class="pl-s1">value</span>
<span class="pl-c1">0</span>  <span class="pl-s1">spam</span>      <span class="pl-c1">1</span>
<span class="pl-c1">3</span>  <span class="pl-s1">spam</span>      <span class="pl-c1">2</span></pre></div>