• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Python / Count Unique Values in pandas DataFrame

Count Unique Values in pandas DataFrame

October 7, 2022 Leave a Comment

To count the unique values of columns in a pandas DataFrame or unique values of a Series, the simplest way is to use the pandas nunique() function.

unique_values = df["variable"].nunique()

When working with data, it’s important to be able to find the basic descriptive statistics of a set of data.

One such piece of information which could be important is the count of unique values in a dataset.

To count unique values in a DataFrame, we can use the pandas nunique() function.

The following code will get you the count of unique values of a single column in Python:

unique_values = df["variable"].nunique()

If you want to count the unique values of an entire DataFrame in pandas, you can call nunique() in the following way:

unique_values = df.nunique()

Count Unique Values in All Columns of DataFrame Using Pandas

Let’s say I have the following pandas DataFrame:

     Name  Weight_Change Month
0     Jim         -16.20     1
1   Sally          12.81     1
2     Bob         -20.45     1
3     Sue          15.35     1
4    Jill         -12.43     1
5   Larry         -18.52     1
6     Jim          -6.10     2   
7   Sally          -2.81     2  
8     Bob          12.45     2
9     Sue          -0.32     2
10   Jill          -1.23     2
11  Larry          -8.52     2
12    Jim           5.20     3 
13  Sally          12.81     3  
14    Bob          -2.45     3
15    Sue           5.35     3
16   Jill          -2.43     3
17  Larry          -1.85     3

We can call the nunique function on the DataFrame to get the number of unique values for all of the columns.

Below shows a simple example of how you can use nunique() to get the count of the unique values for all of the columns in Python.

print(df.nunique())

Name              6
Weight_Change    18
Month             3
dtype: int64

Count Unique Values of Single Column Using Pandas

Let’s say we only want to get the count of unique values of a single column in a pandas DataFrame.

In the DataFrame above, let’s get the count of unique values for the column “Month”. We should expect 3, respectively.

To get the count of unique values of the a single column, you can do the following in your Python code:

print(df["Month"].nunique())

#Output:
3

If you want to get the count of unique values for multiple columns, you can do the following.

In the DataFrame above, let’s get the count of unique values for the columns “Month” and “Name”. We should expect 3 and 6, respectively.

To get the count of unique values of the two columns, you can do the following in your Python code:

print(df[["Month", "Name"]].nunique())

#Output:
3
6
dtype: int64

Hopefully this article has been useful for you to get the count of unique values in a pandas DataFrame using Python.

Other Articles You'll Also Like:

  • 1.  Sum Columns Dynamically with pandas in Python
  • 2.  Get Public IP Address Using Python
  • 3.  Scroll Up Using Selenium in Python
  • 4.  What is the Correct File Extension for Python Files?
  • 5.  Using Python to Find Closest Value in List
  • 6.  Python Print List – Printing Elements of List to the Console
  • 7.  How to Shutdown Computer with Python
  • 8.  Touch File Python – How to Touch a File Using Python
  • 9.  Using Python to Initialize Array of Size N
  • 10.  Using Python to Find Second Smallest Value in List

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy