Learning to Count Unique Values with Pandas GroupBy: A Data Analysis Tutorial


The Foundation of Data Aggregation: Grouped Unique Counting

The core of effective data science lies in the ability to transform raw, voluminous data into concise, actionable summaries. A critical task that frequently arises when performing Exploratory Data Analysis (EDA) is determining the number of distinct entries or unique items present within specific subgroups of a dataset. This requirement moves beyond simple record counting; it measures the underlying diversity and variability across categories. The Pandas library, the cornerstone for data manipulation in Python, offers an exceptionally efficient method for this operation by seamlessly combining the groupby() method with the nunique() aggregation function.

This powerful combination allows analysts to segment a large dataset—typically stored as a DataFrame—based on one or more categorical columns, and then calculate how many unique values exist in a target column within each segment. Understanding this diversity is vital for detecting structural imbalances, assessing data quality, and gaining deeper, category-specific insights that simple overall statistics would obscure. Mastering this technique is fundamental for anyone working with structured data aggregation.

Core Syntax: Utilizing Groupby and Nunique for Efficiency

To effectively compute the unique count per group, Pandas employs a fluent, method-chaining syntax that is both highly optimized and exceptionally readable. This concise command chain first identifies the criteria by which the data should be partitioned and then specifies the subsequent statistical calculation to be performed on the resultant groups. The entire process is handled internally by Pandas, leveraging optimized C-level operations for speed.

The process begins with the groupby() function, which logically separates the DataFrame rows into clusters based on the values in the specified column(s). Once these groups are defined, the aggregation step is executed. We select the column we wish to analyze and apply the nunique() method, which counts the distinct, non-missing values within that column for every predefined group.

The generalized structure for performing this essential unique counting operation is as follows:

df.groupby('group_column')['count_column'].nunique()

In this structure, the group_column parameter dictates the categorical field used for partitioning (e.g., ‘Region’ or ‘Product Type’), while the count_column specifies the metric field whose unique entries we aim to quantify (e.g., ‘Customer ID’ or ‘Transaction Code’). This structure forms the basis of all grouped unique counting operations in Pandas.

Setting Up the Sample Dataset for Demonstration

To practically illustrate how the `groupby()` and `nunique()` methods operate, we will construct a simple, relatable sports statistics dataset. This sample DataFrame contains detailed player records, including their assigned team, their playing position, and their performance metrics such as points scored and rebounds achieved. This structure provides distinct categorical fields (`team`, `position`) and numerical fields (`points`, `rebounds`) suitable for aggregation.

The following Python code initializes this structured dataset using the powerful data structures provided by the Pandas library:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'B'],
                   'position': ['G', 'G', 'G', 'F', 'F', 'G', 'G', 'F', 'F', 'F'],
                   'points': [5, 7, 7, 9, 12, 9, 9, 4, 7, 7],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12, 13, 15]})

#view DataFrame
df

	team	position points	rebounds
0	A	G	 5	11
1	A	G	 7	8
2	A	G	 7	10
3	A	F	 9	6
4	A	F	 12	6
5	B	G	 9	5
6	B	G	 9	9
7	B	F	 4	12
8	B	F	 7	13
9	B	F	 7	15

This dataset, which includes duplicates (e.g., Team A has two players scoring 7 points), is perfectly suited for demonstrating how the nunique() function intelligently ignores repeats, providing a true measure of underlying variety. We will use this foundational structure throughout the subsequent examples to demonstrate single and multi-column grouping.

Example 1: Measuring Diversity with a Single Grouping Column

In many analytical scenarios, the initial step involves assessing the variance of a metric across a primary category. For our sports dataset, a logical inquiry is: “How many distinct scoring totals (points) were recorded by players on Team A versus Team B?” This calculation provides a direct measure of scoring diversity between the two teams, irrespective of the total number of players or records.

To execute this analysis, we simply chain the methods: we call groupby() using only the 'team' column, select the 'points' column for aggregation, and then apply nunique(). This streamlined command yields the desired summary:

#count number of unique values in 'points' column grouped by 'team' column
df.groupby('team')['points'].nunique()

team
A    4
B    3
Name: points, dtype: int64

The resulting Pandas Series confirms the calculated scoring diversity. Team A recorded 4 unique point totals (5, 7, 9, and 12), whereas Team B recorded 3 unique point totals (4, 7, and 9). Importantly, even though two players on Team A scored 7 points, the value 7 is counted only once, fulfilling the definition of a unique count. This simple yet powerful operation provides immediate comparative insight into the variability between groups.

Distinction: Counting Unique Values versus Listing Them

While the nunique() function is essential for quickly summarizing the count of distinct items, analysts often require the actual list of unique values that contributed to that count. Pandas accommodates this need through the complementary unique() function. When chained after a groupby() operation, this method returns the distinct values themselves, encapsulated typically within a list or a NumPy array for each group.

Applying unique() using the same grouping criteria as Example 1 allows us to retrieve the exact point totals that were counted:

#display unique values in 'points' column grouped by 'team'
df.groupby('team')['points'].unique()

team
A    [5, 7, 9, 12]
B        [9, 4, 7]
Name: points, dtype: object

This output visually confirms the previous result: Team A’s unique scores were 5, 7, 9, and 12, resulting in a count of four. Team B’s unique scores were 9, 4, and 7, resulting in a count of three. This ability to switch between numerical summary (count) and qualitative inspection (list of values) is a crucial aspect of thorough data investigation.

Example 2: Granular Analysis Using Multiple Grouping Columns

Data analysis often requires a deeper, more granular level of scrutiny, necessitating segmentation based on more than one categorical variable. In our sports example, we might want to analyze scoring diversity not just by team, but broken down by the player’s 'position' within that team. This allows us to compare, for instance, the scoring variability of Team A’s guards versus Team B’s guards.

To achieve this multi-level grouping, we pass a list of column names (['team', 'position']) to the groupby() function. The rest of the aggregation chain remains identical, applying nunique() to the 'points' column:

#count number of unique values in 'points' column grouped by 'team' and 'position'
df.groupby(['team', 'position'])['points'].nunique()

team  position
A     F           2
      G           2
B     F           2
      G           1
Name: points, dtype: int64

The result is a Pandas Series indexed by a MultiIndex, allowing immediate interpretation of the unique counts for every unique combination. We can observe that while Team A’s Forwards (‘F’) and Guards (‘G’) both recorded 2 unique point totals, Team B’s Guards (‘G’) recorded only 1 unique point total. This finding is highly significant, suggesting a complete lack of scoring variability—every guard on Team B scored the exact same number of points (as confirmed by the raw data, where all Team B Guards scored 9 points).

By applying the unique() function again to this multi-level grouping, we can confirm the specific values contributing to these counts:

#display unique values in 'points' column grouped by 'team' and 'position'
df.groupby(['team', 'position'])['points'].unique()

team  position
A     F           [9, 12]
      G            [5, 7]
B     F            [4, 7]
      G               [9]
Name: points, dtype: object

This detailed output confirms that the single unique count for Team B, position G, is indeed the value 9, providing the complete context necessary for robust analysis and reporting.

Summary and Advanced Aggregation Techniques

The seamless integration of Pandas groupby() and nunique() offers a powerful and indispensable mechanism for data aggregation in Python. This methodology allows analysts to quickly measure data diversity and variability across any defined subset of a DataFrame, whether grouping is based on a single column or a complex combination of categorical factors. The efficiency and clarity of this method chaining make it a core skill for proficient data manipulation.

Beyond simply counting unique values, the `groupby()` operation serves as a gateway to countless other aggregation tasks. To further advance your skills in working with grouped data, consider exploring methods such as .agg(), which allows for simultaneous calculation of multiple statistics (like mean, sum, and unique count) on different columns, or .transform(), which enables applying group-level results back to the original DataFrame structure, facilitating operations like Z-score normalization within subgroups. Mastering these advanced aggregation techniques ensures maximum efficiency in complex data preparation and analysis workflows.

Cite this article

Mohammed looti (2025). Learning to Count Unique Values with Pandas GroupBy: A Data Analysis Tutorial. PSYCHOLOGICAL STATISTICS. Retrieved from https://statistics.arabpsychology.com/count-unique-values-using-pandas-groupby/

Mohammed looti. "Learning to Count Unique Values with Pandas GroupBy: A Data Analysis Tutorial." PSYCHOLOGICAL STATISTICS, 1 Nov. 2025, https://statistics.arabpsychology.com/count-unique-values-using-pandas-groupby/.

Mohammed looti. "Learning to Count Unique Values with Pandas GroupBy: A Data Analysis Tutorial." PSYCHOLOGICAL STATISTICS, 2025. https://statistics.arabpsychology.com/count-unique-values-using-pandas-groupby/.

Mohammed looti (2025) 'Learning to Count Unique Values with Pandas GroupBy: A Data Analysis Tutorial', PSYCHOLOGICAL STATISTICS. Available at: https://statistics.arabpsychology.com/count-unique-values-using-pandas-groupby/.

[1] Mohammed looti, "Learning to Count Unique Values with Pandas GroupBy: A Data Analysis Tutorial," PSYCHOLOGICAL STATISTICS, vol. X, no. Y, ص Z-Z, November, 2025.

Mohammed looti. Learning to Count Unique Values with Pandas GroupBy: A Data Analysis Tutorial. PSYCHOLOGICAL STATISTICS. 2025;vol(issue):pages.

Download Post (.PDF)
Scroll to Top