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

This tutorial illustrates how to count the distinct values by group in the column of a pandas DataFrame in Python.

Preparing the Example

import pandas as pd                               # Import pandas library to Python
my_df = pd.DataFrame({'A':[7, 7, 4, 1, 2, 8, 2],  # Construct example DataFrame in Python
                      'B':[1, 1, 1, 2, 2, 2, 3]})
print(my_df)                                      # Display example DataFrame in console
#    A  B
# 0  7  1
# 1  7  1
# 2  4  1
# 3  1  2
# 4  2  2
# 5  8  2
# 6  2  3

Example: Get Count of Unique Values in Particular pandas DataFrame Variable

print(my_df.groupby('B')['A'].nunique())          # Count unique values in column
# B
# 1    2
# 2    3
# 3    1

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.
You need to agree with the terms to proceed

Menu
Top