Count Unique Values in Pandas • datagy (2024)

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 DataFrameimport pandas as pddf = 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:

  1. axis= to count unique values in either columns or rows
  2. dropna= 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 Columnprint(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 Columnsprint(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 Valuesnum_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 DataFrameprint(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 Pandasprint(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:

  • Pandas: Count Unique Values in a GroupBy Object
  • Python: Count Unique Values in a List (4 Ways)
  • All the Ways to Get Pandas Unique Values
  • Summarizing and Analyzing a Pandas DataFrame
  • Pandas nunique: Official Documentation
Count Unique Values in Pandas • datagy (2024)

References

Top Articles
Latest Posts
Article information

Author: Ouida Strosin DO

Last Updated:

Views: 6337

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Ouida Strosin DO

Birthday: 1995-04-27

Address: Suite 927 930 Kilback Radial, Candidaville, TN 87795

Phone: +8561498978366

Job: Legacy Manufacturing Specialist

Hobby: Singing, Mountain biking, Water sports, Water sports, Taxidermy, Polo, Pet

Introduction: My name is Ouida Strosin DO, I am a precious, combative, spotless, modern, spotless, beautiful, precious person who loves writing and wants to share my knowledge and understanding with you.