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

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

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.

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.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

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

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

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.