In this tutorial, we will look at how to get a list of unique values in a pandas dataframe column. Additionally, we will also look at how to get a count of each unique value within the column and the total unique count.
First, let’s create a sample dataframe that we will be using throughout this tutorial for demonstrating the usage.
import pandas as pd # dataframe of height and weight football players df = pd.DataFrame({ 'Height': [167, 175, 170, 186, 190, 188, 158, 169, 183, 180], 'Weight': [65, 70, 72, 80, 86, 94, 50, 58, 78, 85], 'Team': ['A', 'A', 'B', 'B', 'B', 'C', 'A', 'C', 'B', 'C'] }) # display the dataframe print(df)
Output:
Height Weight Team 0 167 65 A 1 175 70 A 2 170 72 B 3 186 80 B 4 190 86 B 5 188 94 C 6 158 50 A 7 169 58 C 8 183 78 B 9 180 85 C
Now we have a dataframe with 10 rows and three columns storing information on height, weight, and teams of top scorers in a football competition.
1. List of all unique values in a pandas dataframe column
You can use the pandas unique()
function to get the different unique values present in a column. It returns a numpy array of the unique values in the column. For example, let’s see what are the unique values present in the column “Team” of the dataframe “df” created above.
# unique values in column "Team" print(df['Team'].unique()) # check the return type print(type(df['Team'].unique()))
Output:
['A' 'B' 'C'] <class 'numpy.ndarray'>
You can see we get a numpy array of all the unique values present in the column “Team” – “A”, “B”, and “C”.
For more on the pandas unique() function, refer to its documentation.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
2. Count of unique values in a column
If you just need the count of unique values present in a pandas dataframe column, you can use the pandas nunique()
function. It returns the number of unique values present in the dataframe as an integer. For example, let’s count the number of unique values in the column “Team” of the dataframe “df”.
# count of unique values print(df['Team'].nunique())
Output:
3
We get 3
as the output because there are three unique values present in the column “Team”. Note that you can use the nunique() function to count the unique values in a row as well.
3. Count of each unique value in a column
You can use the pandas value_counts()
function to get the number of times each unique value occurs in a column. For example, let’s find the what’s the count of each unique value in the “Team” column.
# value counts of each unique value print(df['Team'].value_counts())
Output:
B 4 A 3 C 3 Name: Team, dtype: int64
You can see that the value “B” occurs 4 times, and “A” and “C” occur 3 times each in the column “Team”. The value_counts() is a nice function to check the distribution of data points across a categorical field.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Tutorials on common column operations in pandas –