0
0
Pandasdata~5 mins

nunique() for unique counts in Pandas

Choose your learning style9 modes available
Introduction

We use nunique() to count how many different values are in a list or column. It helps us understand variety in data.

Counting how many different products customers bought.
Finding how many unique cities appear in an address list.
Checking how many unique dates are in a sales record.
Seeing how many different categories exist in a dataset.
Syntax
Pandas
DataFrame['column_name'].nunique(dropna=True)

# or for whole DataFrame
DataFrame.nunique(dropna=True)

dropna=True means it ignores empty or missing values when counting.

You can use nunique() on a single column or on the whole DataFrame.

Examples
Count unique cities in the 'City' column.
Pandas
df['City'].nunique()
Count unique values in each column of the DataFrame.
Pandas
df.nunique()
Count unique products including missing values as a unique category.
Pandas
df['Product'].nunique(dropna=False)
Sample Program

This code creates a small table with names, cities, and products. It then counts how many unique names, cities, and products are in the table. For products, it counts missing values as unique if any.

Pandas
import pandas as pd

data = {
    'Name': ['Alice', 'Bob', 'Alice', 'David', 'Bob', None],
    'City': ['New York', 'Los Angeles', 'New York', 'Chicago', 'Chicago', 'Chicago'],
    'Product': ['Book', 'Pen', 'Book', 'Pen', 'Notebook', 'Pen']
}

df = pd.DataFrame(data)

# Count unique names ignoring missing values
unique_names = df['Name'].nunique()

# Count unique cities
unique_cities = df['City'].nunique()

# Count unique products including missing values
unique_products_incl_na = df['Product'].nunique(dropna=False)

print(f"Unique Names: {unique_names}")
print(f"Unique Cities: {unique_cities}")
print(f"Unique Products (including NA): {unique_products_incl_na}")
OutputSuccess
Important Notes

If you want to count missing values as unique, set dropna=False.

By default, nunique() ignores missing values.

Use nunique() to quickly understand diversity in your data.

Summary

nunique() counts how many different values are in data.

It can be used on one column or the whole DataFrame.

Missing values are ignored by default but can be included.