How to list unique values in a Pandas DataFrame?

This recipe helps you list unique values in a Pandas DataFrame.

Objective For ‘How to list unique values in a Pandas DataFrame?’

This step-by-step recipe will show you how to find unique values in Python Dataframe and list them.

How To Check Unique Values in Python Dataframe?

We can easily view the dataframe, and sometimes we find that a few values have been repeated many times in different rows. What should we do if we need to find unique values or categories in the feature? 

This recipe will help you learn how to make a list of unique values in Pandas DataFrame.

Get Closer To Your Dream of Becoming a Data Scientist with 70+ Solved End-to-End ML Projects

Step 1 - Import the library

import pandas as pd

We have only imported the Pandas library, which is required for this.

Step 2 - Setting up the Data

We have created a dictionary with columns 'Name', 'Year', and 'Episodes' and passed this in pd.DataFrame to create a DataFrame with index. 

data = {'name': ['Sheldon', 'Penny', 'Amy', 'Penny', 'Raj', 'Sheldon'], 'year': [2012, 2012, 2013, 2014, 2014,2012 ], 'episodes': [42, 24, 31, 29, 37, 40]} df = pd.DataFrame(data, index = ['a', 'b', 'c', 'd', 'e','f']) print(df)

Explore More Data Science and Machine Learning Projects for Practice. Fast-Track Your Career Transition with ProjectPro

Step 3 - Pandas List Unique Values and Printing it

We can find unique values by unique function in two formats:

  • series.unique(): In this, we have to add the unique function after the series(column) in which we want to find the unique values.

  • pd.unique(): In this, we have to pass the series as a parameter to find the unique values.

We have used both functions for better understanding. 

print(df.name.unique()) print(pd.unique(df['year'])) 

The output of the dataset comes as-

     name  year  episodes

a  Sheldon  2012        42

b    Penny  2012        24

c      Amy  2013        31

d    Penny  2014        29

e      Raj  2014        37

f  Sheldon  2012        40

 

['Sheldon' 'Penny' 'Amy' 'Raj']

 

[2012 2013 2014]

FAQs

  1. How to list unique values in a Pandas DataFrame column?

To convert Pandas unique values to list in a DataFrame column, you can use the unique() method. The unique() method takes a DataFrame column as its input and returns a list of the unique values in the column. For example, the following code lists the unique values in the column_name column of a DataFrame called ‘df’: df['column_name'].unique()

  1. How to list unique values in a Pandas DataFrame row?

To make a Pandas unique list of values in a DataFrame row, you can use the df.stack().unique() method. The df.stack() method stacks the DataFrame rows one on top of the other, and the unique() method returns a list of the unique values in the stacked DataFrame. For example, the following code lists the unique values in each row of a DataFrame called df: df.stack().unique()

  1. How to list unique values in a Pandas DataFrame with multiple columns?

To make a Pandas DataFrame unique list of values in a dataframe with multiple columns, you can use the df.nunique() method. The df.nunique() method takes a list of column names as its input and returns a DataFrame with the number of unique values in each column. For example, the following code lists the number of unique values in the column_name_1 and column_name_2 columns of a DataFrame called df: df[['column_name_1', 'column_name_2']].nunique()

 Join Millions of Satisfied Developers and Enterprises to Maximize Your Productivity and ROI with ProjectPro - Read ProjectPro Reviews Now!  

 

 

Download Materials

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

Classification Projects on Machine Learning for Beginners - 2
Learn to implement various ensemble techniques to predict license status for a given business.