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 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:
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.
If you accept this notice, your choice will be saved and the page will refresh.
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).
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.
If you accept this notice, your choice will be saved and the page will refresh.
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.
- Count Unique Values by Group in Column of pandas DataFrame
- Get List of Column Names Grouped by Data Type in Python
- Manipulate pandas DataFrames in Python
- Handling DataFrames Using the pandas Library in Python
- Python Programming Overview
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.