How to list unique values in a Pandas DataFrame?

How to list unique values in a Pandas DataFrame?

How to list unique values in a Pandas DataFrame?

This recipe helps you list unique values in a Pandas DataFrame

Recipe Objective

We can easily view the dataframe and sometimes we find that few of the values have been repeated many times in different rows. So if we need to find unique values or categories in the feature then what to do ?

So this is the recipe on How we can make a list of unique values in a Pandas DataFrame.

Step 1 - Import the library

import pandas as pd

We have only imported pandas 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)

Step 3 - Finding 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( print(pd.unique(df['year'])) 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]

Download Materials

Relevant Projects

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

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.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Census Income Data Set Project - Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based on census data.

Inventory Demand Forecasting using Machine Learning in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.