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

Time Series Classification Project for Elevator Failure Prediction
In this Time Series Project, you will predict the failure of elevators using IoT sensor data as a time series classification machine learning problem.

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

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.

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

Llama2 Project for MetaData Generation using FAISS and RAGs
In this LLM Llama2 Project, you will automate metadata generation using Llama2, RAGs, and AWS to reduce manual efforts.

Data Analysis of Working Capital Management using Tableau
In this Data Analysis Project using Tableau, you will focus on optimizing working capital by analyzing receivables and payables data using Tableau and build actionable dashboards.

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Build PowerBI Dashboard for Water Quality Sensor Data Analysis
In this PowerBI Project, you will learn to build a PowerBI Dashboard to analyze and visualize water quality sensor data from various European countries.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT