In this tutorial, you’ll learn how to use Pandas to count unique values. You’ll learn how to count unique values in a column, in multiple columns, and in an entire DataFrame. Being able to count unique values can have important use cases. For example, this can be used in testing your code. Similarly, it can be used in machine learning projects. Want to learn how to find unique values instead? Check out this guide to the unique() method.
By the end of this tutorial, you’ll have learned:
- How to count unique values in Pandas using the nunique method
- How to count unique values in multiple columns or an entire DataFrame
- How to count the frequency of a unique value in Pandas
Table of Contents
Loading a Sample Pandas DataFrame
To follow along with this tutorial, copy and paste the code below to load a sample Pandas DataFrame. If you’re working with your own data, feel free to use that. Your results, then, will of course vary. Let’s get started:
# Loading a Sample Pandas DataFrame
import pandas as pd
df = pd.DataFrame.from_dict({
'Name': ['Jane', 'Nik', 'Kate', 'Melissa', 'Evan', 'Doug', 'Joe'],
'Age': [10, 35, 34, 23, 70, 55, 89],
'Gender': ['Female', 'Male', 'Female', 'Female', 'Male', 'Male', 'Male']
})
print(df.head())
# Returns:
# Name Age Gender
# 0 Jane 10 Female
# 1 Nik 35 Male
# 2 Kate 34 Female
# 3 Melissa 23 Female
# 4 Evan 70 Male
In the following sections, you’ll learn how to count unique values in a DataFrame.
How to Count Unique Values in a Pandas DataFrame Column Using nunique
In this section, you’ll learn how to use the nunique method to count the number of unique values in a DataFrame column. The method takes two parameters:
axis=
to count unique values in either columns or rowsdropna=
whether to include missing values in the counts or not
Let’s see how we can use the .nunique()
method to count the number of unique values in the 'Name'
column:
# Counting Unique Values in a Single Pandas DataFrame Column
print(df['Name'].nunique())
# Returns: 7
We can see that the method returns 7
, meaning that there are seven unique values in that column. In the following section, you’ll learn how to count unique values in multiple columns of a DataFrame.
How to Count Unique Values in Multiple Pandas DataFrame Columns Using nunique
Similar to the example above, you can apply the .nunique()
method to multiple columns. To do this, simply access the columns you want to count using double squared brackets.
When applying the method to more than a single column, a Pandas Series is returned. Let’s take a look at how this works:
# Counting Unique Values in Multiple Pandas DataFrame Columns
print(df[['Name', 'Gender']].nunique())
# Returns:
# Name 7
# Gender 2
# dtype: int64
Because a Pandas Series is returned, we can actually access the counts of unique values by indexing that Series. Let’s see how that works:
# Accessing the Count of Unique Values
num_unique = df[['Name', 'Gender']].nunique()
print(num_unique['Gender'])
# Returns: 2
How to Count Unique Values in a Pandas DataFrame Using nunique
The .nunique()
method can also be applied to an entire DataFrame. Similar to counting unique values in multiple DataFrame columns, the method will return a Pandas Series of counts of unique values. Let’s see how this works to apply to an entire DataFrame:
# Counting Unique Values in an Entire Pandas DataFrame
print(df.nunique())
# Returns:
# Name 7
# Age 7
# Gender 2
# dtype: int64
In the final section of this tutorial, you’ll learn how to count the frequency of unique values in a DataFrame.
How to Count the Frequency of Unique Values in Pandas
In this section, you’ll learn how to count the frequency of unique values in a DataFrame. This gives you a sense of the distribution of values across a column.
In order to do this, we can use the .value_counts()
function, which generates a frequency table of the column. Let’s see how we can count the unique values in our 'Gender'
column:
# Count the Frequency of Unique Values in Pandas
print(df['Gender'].value_counts())
# Returns:
# Male 4
# Female 3
# Name: Gender, dtype: int64
We can see the unique values as the index of our Series, where the values are the number of times each value appears in the column. For example, 'Male'
appears four times.
Conclusion
In this tutorial, you learned how to count unique values in a DataFrame column. You first learned how to use the .nunique()
method to count the number of unique values in a Pandas column, multiple columns, and an entire DataFrame. You also learned how to count the frequency of unique values in a Pandas DataFrame column using the .value_counts()
method.
Additional Resources
To learn more about related topics, check out the tutorials below: