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

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

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.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

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.

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

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.