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


In this Python tutorial you’ll learn how to count the number of unique elements in a pandas DataFrame column.

The content of the post looks as follows:

It’s time to dive into the example!


Example Data & Software Libraries

In order to apply the functions of the pandas library, we first have to load pandas.

import pandas as pd                                         # Load pandas library

We’ll also need to create some example data:

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 column pandas dataframe python


Table 1 shows the structure of our exemplifying data: It comprises nine rows and two columns.


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

In this section, I’ll explain how to count the unique values in a specific variable of a pandas DataFrame using the Python programming language.

For this task, we can apply the nunique function as shown in the following code:

count_unique = data['values'].nunique()                     # Apply unique function
print(count_unique)                                         # Print count of unique values
# 3

The Python console has returned the value 3, i.e. the variable “values” contains three different unique values.

Note that we have calculated the number of unique values in an entire DataFrame column. In case you want to count unique values by the groups of a pandas DataFrame, you may have a look here.


Video, Further Resources & Summary

This written article wasn’t clear enough? Then please have a look at the following tutorial video which I have published on my YouTube channel. I show and explain the examples of this post in some more detail:



In case you are interested in further information on how to count values in Python, you may also have a look at the following YouTube video of the Tech With Tim YouTube channel. In the video, the speaker explains how to use the count() and find() functions (two related functions to the nunique function that we have used in this tutorial).



Furthermore, you might read the related articles on Statistics Globe. You can find some tutorials that are related to the counting of the number of unique elements in a pandas DataFrame variable below.


At this point you should know how to count the number of unique and distinct elements in a pandas DataFrame variable in the Python programming language. In case you have additional questions, tell me about it in the comments.


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.