The nunique()
method in Pandas returns the number of unique values over the specified axis.
Example
import pandas as pd
# sample DataFrame
data = {'A': [1, 2, 2],
'B': [4, 5, 6]}
df = pd.DataFrame(data)
# calculate the number of unique values in each column
unique_values = df.nunique()
print(unique_values)
'''
Output
A 2
B 3
dtype: int64
'''
nunique() Syntax
The syntax of the nunique()
method in Pandas is:
df.nunique(axis=0, dropna=True)
nunique() Arguments
The nunique()
method has the following arguments:
axis
(optional): the axis to compute the number of unique values alongdropna
(optional): ifFalse
,NaN
values are also counted
nunique() Return Value
The nunique()
method returns a scalar if applied to a Series or a Series if applied to a DataFrame.
Example 1: Counting Unique Values in a Series
import pandas as pd
# sample Series
data = pd.Series([1, 2, 2, 3, 3, 3])
# calculate the number of unique values
unique_count = data.nunique()
print(unique_count)
Output
3
Here, we calculated the number of unique values in a Series.
Example 2: Including NaN values in the Count
import pandas as pd
# sample DataFrame
data = {'A': [1, 2, None],
'B': [4, None, None]}
df = pd.DataFrame(data)
# calculate the number of unique values including nan
unique_count = df.nunique(dropna=False)
print(unique_count)
Output
A 3 B 2 dtype: int64
In this example, we set dropna=False
to include NaN
values in the count of unique values.
Example 3: Unique Values in Rows
import pandas as pd
# sample DataFrame
data = {'A': [1, 2, 3],
'B': [4, 2, 1]}
df = pd.DataFrame(data)
# calculate the number of unique values in rows
unique_count = df.nunique(axis=1)
print(unique_count)
Output
0 2 1 1 2 2 dtype: int64
In this example, we changed the axis
to 1 to count unique values across rows.