How to list unique values in a Pandas DataFrame?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

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

0

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(df.name.unique()) 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]

Relevant Projects

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
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.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

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.

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.

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.

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.

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

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.