Count Unique Values by Group in Column of pandas DataFrame in Python (Example)


In this Python programming tutorial you’ll learn how to count the distinct values by group in the column of a pandas DataFrame.

The article contains these contents:

Here’s the step-by-step process…


Example Data & Libraries

If we want to use the functions of the pandas library, we first need to load pandas:

import pandas as pd                                         # Import pandas

We’ll also have to construct some data that we can use in the examples below:

data = pd.DataFrame({'values':[1, 2, 3, 1, 1, 1, 1, 2, 2],  # Create example DataFrame
                     'groups':['a', 'a', 'a', 'a', 'b', 'b', 'c', 'c', 'c']})
print(data)                                                 # Print example DataFrame


table 1 DataFrame count unique values group column pandas dataframe python


As you can see based on Table 1, our exemplifying data is a DataFrame made of nine rows and the two columns “values” and “groups”.


Example: Count Unique Values in Column of pandas DataFrame Using nunique() Function

The following Python programming code explains how to count the number of different values in a pandas DataFrame column by group.

For this task, we can use the groupby and nunique functions as shown below:

count_unique = data.groupby('groups')['values'].nunique()   # Apply unique function
print(count_unique)                                         # Print count of unique values
# groups
# a    3
# b    1
# c    2
# Name: values, dtype: int64

Have a look at the previous output of the Python console: The group a contains three different values, the group b consists of only one distinct value (i.e. all values are identical), and the group c contains two unique elements.


Video & Further Resources

The educational YouTuber Corey Schafer has recently published a video on his YouTube channel, which explains how to group and aggregate data in the Python programming language. The examples shown in his video might also be helpful to understand the code of this tutorial. You can find the video below:


Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.


Furthermore, you may want to read the other articles on this website. You can find some articles below.


To summarize: You have learned on this page how to get the count of unique values by group in a particular variable of a pandas DataFrame in the Python programming language. In case you have additional questions, don’t hesitate to tell me about it in the comments section.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.