Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
Pandas nunique()
In this tutorial, you will learn about the nunique() method in Pandas with the help of examples.
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,NaNvalues 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.