4️⃣ 🧹 Pandas Data Cleaning & Preprocessing
Estimated reading: 3 minutes 50 views

🔢 Pandas Counting Unique Values – Analyze Distinct Entries Easily


🧲 Introduction – Why Count Unique Values?

Knowing how many unique values exist in a dataset is crucial for data exploration, validation, and summarization. Whether you’re analyzing categories, IDs, names, or labels, Pandas makes it easy to count distinct values, their frequencies, and even rank them by occurrence.

🎯 In this guide, you’ll learn:

  • How to count unique values in Series and DataFrames
  • Use .nunique() and .value_counts() effectively
  • Count across rows or columns
  • Handle missing values during counting

📥 1. Sample DataFrame for Practice

import pandas as pd

df = pd.DataFrame({
    'Department': ['Sales', 'HR', 'HR', 'IT', 'Sales', 'Sales', 'IT'],
    'Employee': ['Alice', 'Bob', 'Charlie', 'Alice', 'Eve', 'Frank', 'Charlie'],
    'Status': ['Active', 'Active', 'Resigned', 'Resigned', 'Active', 'Resigned', 'Active']
})

print(df)

👉 Output:

  Department Employee    Status
0      Sales    Alice    Active
1         HR      Bob    Active
2         HR  Charlie  Resigned
3         IT    Alice  Resigned
4      Sales      Eve    Active
5      Sales     Frank  Resigned
6         IT  Charlie    Active

🧮 2. Count Unique Values in a Column

df['Employee'].nunique()

✔️ Returns the number of unique employee names.

👉 Output:

5

📊 3. Count Frequency of Each Unique Value

df['Department'].value_counts()

✔️ Returns a Series showing how often each department appears.

👉 Output:

Sales    3
HR       2
IT       2
Name: Department, dtype: int64

🧠 4. Count Unique Values in Each Column

df.nunique()

✔️ Counts unique values column-wise for the entire DataFrame.

👉 Output:

Department    3
Employee      5
Status        2
dtype: int64

🔁 5. Count Unique Values Across Rows

df.nunique(axis=1)

✔️ Counts unique entries in each row.


🧹 6. Include or Exclude NaN in Counts

df['Employee'].nunique(dropna=False)

✔️ Set dropna=False to include NaN in the count (if present).


📈 7. Use value_counts() with Normalization and Sorting

df['Status'].value_counts(normalize=True, ascending=True)

✔️ Shows the relative frequencies (as percentages), sorted in ascending order.

👉 Output:

Resigned    0.428571
Active      0.571429
Name: Status, dtype: float64

🧾 8. Count Values with Custom Binning

df['Name_Length'] = df['Employee'].str.len()
df['Name_Length'].value_counts(bins=3)

✔️ Groups value counts into bin ranges, ideal for numerical distributions.


📌 Summary – Key Takeaways

Counting unique values is a core part of data profiling and cleaning. Pandas offers .nunique() for distinct counts and .value_counts() for frequency analysis—both highly customizable.

🔍 Key Takeaways:

  • Use .nunique() for count of distinct values
  • Use .value_counts() to count frequency of each value
  • Works on Series, rows, or entire DataFrames
  • Customize with normalize, dropna, bins, sort

⚙️ Real-world relevance: Used in survey analysis, category distribution, feature engineering, and data validation.


❓ FAQs – Counting Unique Values in Pandas

❓ How do I count unique rows across the entire DataFrame?

df.drop_duplicates().shape[0]

❓ Can I count how many times each value appears in multiple columns?
Use .apply(value_counts):

df[['Department', 'Status']].apply(pd.Series.value_counts)

❓ How do I get the most frequent value?

df['Employee'].value_counts().idxmax()

❓ Can I include NaN as a unique value?
✅ Yes:

df['Column'].nunique(dropna=False)

❓ What if I want counts in percentages?
Use:

df['Column'].value_counts(normalize=True)

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Pandas Counting Unique Values

Or Copy Link

CONTENTS
Scroll to Top