• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / pandas unique – Get Unique Values in Column of DataFrame

pandas unique – Get Unique Values in Column of DataFrame

December 28, 2021 Leave a Comment

To get the unique values of a column in pandas, the simplest way is to use the pandas unique() function.

df["variable"].unique()

You can also use the pandas unique() function in the following way:

pd.unique(series)

When working with data as a data science or data analyst, it’s sometimes important to be able to easily find the unique values of your dataset.

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

The following code will get you the unique values of a series in Python:

df["variable"].unique()

If you want to get the number of unique values of an entire DataFrame in pandas, you can call pandas nunique() function.

Getting the Unique Values in a Column 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 unique() function on “Name” column to find the unique values for that column.

print(df["Name"].unique())

array(['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'], dtype=object)

We can also find the unique values in the “Name” column in the following way:

print(pd.unique(df["Name"]))

array(['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'], dtype=object)

You can see that this returns the same array as above.

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

Other Articles You'll Also Like:

  • 1.  Writing Multiple Lines Lambda Expression in Python
  • 2.  nunique pandas – Get Number of Unique Values in DataFrame
  • 3.  Selenium minimize_window() Function to Minimize Window in Python
  • 4.  Generate Random Float Between 0 and 1 Using Python
  • 5.  Using Python to Get and Print First N Items in List
  • 6.  Remove Duplicates from Sorted Array in Python
  • 7.  Creating a List of Zeros in Python
  • 8.  Using Python to Sum Odd Numbers in List
  • 9.  Python Print List – Printing Elements of List to the Console
  • 10.  Writing a Table Faster to Word Document Using python-docx

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 *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

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