title: Python Pandas: count unique values in row
body: <p>I have a large (16000 rows) dataframe like <code>df</code> below. I want to generate a new column which counts the number of unique values in each row without iterating through each row. </p>
  
  <p>So far, I have used the <code>pd.Series.value_counts</code> method with <code>pd.DataFrame.apply()</code> and <code>pd.DataFrame.sum()</code>. But when there are repeating values in the dataframe, this will not get the correct result. How do I count the number of unique values in each row of my dataframe without iterating through it?</p>
  
  <pre><code>&gt;&gt;&gt; import pandas as pd
  &gt;&gt;&gt; import numpy as np
  &gt;&gt;&gt; df = pd.DataFrame(np.random.randn(5,5))
  &gt;&gt;&gt; df
        0         1         2         3         4
  0  0.429437 -0.931365  0.182996  0.627381 -0.166898
  1  1.094186  0.080042  0.924192  0.120056  0.667423
  2 -0.439858  1.681652 -1.753300  0.390554  0.561703
  3  1.579391  1.377110  0.892134  1.704882  0.410462
  4 -0.113080 -0.726894  1.904882 -0.800783  0.709755
  &gt;&gt;&gt; df['unique'] = df.apply(pd.Series.value_counts, axis=1).sum(axis=1)
  &gt;&gt;&gt; df
        0         1         2         3         4  unique
  0 -0.964615  0.039836 -0.004675  0.067316  0.266369       5
  1 -1.582173 -1.329946 -0.597782  1.856062  1.056681       5 
  2 -0.566920 -0.107541  0.672243 -1.374591 -0.243655       5
  3  0.357264  1.662374 -1.477057  0.629704  0.436156       5
  4 -0.318111 -0.472437 -0.222260  0.463293 -0.394258       5
  </code></pre>