Pandas DataFrame | nunique method
Start your free 7-days trial now!
Pandas DataFrame.nunique(~)
method counts the number of unique values for each row or column in the DataFrame.
Parameters
1. axis
link | int
or string
The axis along which to count the number of unique values:
Value | Meaning |
---|---|
| Count each column. |
| Count each row. |
By default, axis=0
.
2. dropna
link | boolean
| optional
Whether or not to ignore NaN
. By default, dropna=True
.
Return Value
A Series
that holds the count of unique numbers in each row or column of the source DataFrame.
Examples
Consider the following DataFrame:
df
A B C0 5 2 11 5 2 22 5 3 4
Counting column-wise
To count the number of unique numbers in each column:
df.nunique() # axis=0
A 1B 2C 3dtype: int64
This tells us that:
column
A
has 1 unique value.column
B
has 2 unique values.column
C
has 3 unique values.
Counting row-wise
To count the number of unique numbers in each row, set axis=1
:
df.nunique(axis=1)
0 21 22 3dtype: int64
This tells us that:
row
0
has 2 unique values.row
1
has 2 unique values.row
2
has 3 unique values.
Dealing with missing values
Consider the following DataFrame with two missing values:
df
A0 NaN1 1.02 NaN
By default, dropna=True
, which means that NaN
s will be ignored:
df.nunique()
A 1dtype: int64
We can consider them as values by setting dropna=False
:
df.nunique(dropna=False)
A 2dtype: int64
This tell us that column A
has 2 unique values: NaN
and 1.0
.