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
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
Do you need further info on the Python code of this article? Then you might have a look at the following video on my YouTube channel. In the video, I’m explaining the content of this post in 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.
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.
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.
- Handling DataFrames Using the pandas Library in Python
- Count Unique Values in Column of pandas DataFrame
- Get List of Column Names Grouped by Data Type in Python
- Manipulate pandas DataFrames in Python
- Introduction to Python Programming
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.