How do you get the count of all unique values of a categorical column in a DataFrame?

Quick Overview

Asks how to count how often each distinct value appears in a categorical pandas column. Tests knowledge of value_counts and its options for missing values, proportions and ordering, plus related tools such as groupby size and nunique.

How do you get the count of all unique values of a categorical column in a DataFrame?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

How do you get the count of all unique values of a categorical column in a DataFrame? ```hint Beyond the one-liner There is a single Series method for this. Once you have it, check how it treats missing values and in what order it returns the results. ``` ### What a Strong Answer Covers - The method that returns each distinct value with its count, and the shape of its result - How missing values are treated and how to include them - Options for proportions and for ordering - Related approaches, such as grouping or counting how many distinct values there are ### Follow-up Questions - How would you count unique combinations of two columns? - The column has a categorical dtype with a category that never occurs. What does `value_counts()` show for it? - How would you turn the result into a DataFrame with one column for the value and one for its count? - How would you keep the top 5 values and lump the rest into "Other"?

Overview: Asks how to count how often each distinct value appears in a categorical pandas column. Tests knowledge of value_counts and its options for missing values, proportions and ordering, plus related tools such as groupby size and nunique.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

How do you get the count of all unique values of a categorical column in a DataFrame?

What a Strong Answer Covers Guidance

  • The method that returns each distinct value with its count, and the shape of its result
  • How missing values are treated and how to include them
  • Options for proportions and for ordering
  • Related approaches, such as grouping or counting how many distinct values there are

Follow-up Questions Guidance

  • How would you count unique combinations of two columns?
  • The column has a categorical dtype with a category that never occurs. What does value_counts() show for it?
  • How would you turn the result into a DataFrame with one column for the value and one for its count?
  • How would you keep the top 5 values and lump the rest into "Other"?