What if you could instantly know how many unique customers you have without counting one by one?
Why nunique() for cardinality in Data Analysis Python? - Purpose & Use Cases
Imagine you have a huge list of customer names and you want to know how many unique customers visited your store last month.
You try to count each unique name by scanning the list manually or using basic tools like Excel without special functions.
Counting unique items manually is slow and tiring, especially when the list is very long.
You might miss duplicates or count some names twice by mistake, leading to wrong results.
This makes your analysis unreliable and wastes your time.
The nunique() function quickly counts how many unique values are in a column or list.
It does the hard work for you, so you get accurate results instantly without errors.
unique_count = len(set(customer_list))unique_count = df['customer_name'].nunique()With nunique(), you can instantly find the number of unique entries in your data, making your analysis faster and more accurate.
A store manager uses nunique() to find out how many different customers visited during a sale, helping plan better promotions next time.
Manually counting unique values is slow and error-prone.
nunique() automates counting unique items accurately and quickly.
This helps you analyze data with confidence and save time.
Practice
nunique() function do when applied to a pandas DataFrame column?Solution
Step 1: Understand the purpose of
Thenunique()nunique()function counts how many distinct or unique values exist in a column.Step 2: Compare with other functions
Unlike sum or max,nunique()focuses on counting unique entries, not numerical calculations.Final Answer:
Counts the number of unique values in that column -> Option DQuick Check:
nunique()= unique count [OK]
nunique() counts unique items only [OK]- Confusing
nunique()withcount() - Thinking it sums values instead of counting unique ones
- Assuming it returns total rows instead of unique values
df?Solution
Step 1: Access the column correctly
To applynunique()on a column, first select the column usingdf['Age'].Step 2: Apply the
Then callnunique()methodnunique()as a method:df['Age'].nunique().Final Answer:
df['Age'].nunique() -> Option BQuick Check:
Correct syntax = df['column'].nunique() [OK]
nunique() [OK]- Passing column name as argument to
nunique() - Using dot notation with parentheses on column name
- Trying to index
nuniquelike a dictionary
df['City'].nunique()?
import pandas as pd
data = {'City': ['Paris', 'London', 'Paris', 'Berlin', 'London', 'Rome']}
df = pd.DataFrame(data)Solution
Step 1: Identify unique cities in the list
The cities are: 'Paris', 'London', 'Paris', 'Berlin', 'London', 'Rome'. Unique cities are 'Paris', 'London', 'Berlin', and 'Rome'.Step 2: Count unique cities
There are 4 unique cities in total.Final Answer:
4 -> Option AQuick Check:
Unique cities count = 4 [OK]
- Counting all entries instead of unique ones
- Miscounting duplicates as unique
- Confusing number of rows with unique count
import pandas as pd
data = {'Product': ['A', 'B', 'A', 'C']}
df = pd.DataFrame(data)
unique_count = df.nunique('Product')Solution
Step 1: Check how
When called on a DataFrame,nunique()works on DataFramesnunique()counts unique values for each column and does not accept column names as arguments.Step 2: Correct way to count unique values in one column
To count unique values in 'Product', select the column first:df['Product'].nunique().Final Answer:
Thenunique()method does not take column names as arguments -> Option AQuick Check:
nunique()on DataFrame has no column argument [OK]
nunique() on column, not DataFrame with argument [OK]- Passing column name as argument to
nunique()on DataFrame - Calling
nuniquewithout parentheses - Misunderstanding DataFrame constructor syntax
df with a column 'Score' containing some missing values (NaN). How do you count unique scores including the missing values as a unique category?Solution
Step 1: Understand
By default,dropnaparameter innunique()nunique()ignores missing values (NaN). Settingdropna=Falsecounts NaN as a unique value.Step 2: Apply
Usenunique()withdropna=Falsedf['Score'].nunique(dropna=False)to include NaN in the unique count.Final Answer:
df['Score'].nunique(dropna=False) -> Option CQuick Check:
Include NaN by setting dropna=False [OK]
dropna=False to count NaN as unique [OK]- Ignoring
dropnaand missing NaN count - Using
count()which excludes NaN - Using
unique()without counting length
Ready to prove it?
Pass the test and this pattern moves unknown
