How can I find the unique values in a column using Pandas?

Pandas is a popular Python library used for data manipulation and analysis. It provides efficient and easy-to-use tools for handling large datasets. One common task in data analysis is finding unique values in a column. To accomplish this, Pandas offers the “unique()” function which returns an array of all the unique values in a specific column. This function can be applied to a Pandas DataFrame or Series, making it a convenient and effective way to identify distinct values in a dataset. By utilizing the “unique()” function, users can quickly gain insights into the unique elements present in a column, which can be useful for data cleaning, filtering, and further analysis.

Pandas: Find Unique Values in a Column


The easiest way to obtain a list of unique values in a pandas DataFrame column is to use the function.

This tutorial provides several examples of how to use this function with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'B', 'B', 'C'],
                   'conference': ['East', 'East', 'East', 'West', 'West', 'East'],
                   'points': [11, 8, 10, 6, 6, 5]})

#view DataFrame
df

        team	conference  points
0	A	East	    11
1	A	East	    8
2	A	East	    10
3	B	West	    6
4	B	West	    6
5	C	East	    5

Find Unique Values in One Column

The following code shows how to find the unique values in a single column of the DataFrame:

df.team.unique()

array(['A', 'B', 'C'], dtype=object)

We can see that the unique values in the team column include “A”, “B”, and “C.”

Find Unique Values in All Columns

The following code shows how to find the unique values in all columns of the DataFrame:

for col in df:
  print(df[col].unique())

['A' 'B' 'C']
['East' 'West']
[11  8 10  6  5]

Find and Sort Unique Values in a Column

The following code shows how to find and sort by unique values in a single column of the DataFrame:

#find unique points values
points = df.points.unique()

#sort values smallest to largest
points.sort()

#display sorted values
points

array([ 5,  6,  8, 10, 11])

Find and Count Unique Values in a Column

The following code shows how to find and count the occurrence of unique values in a single column of the DataFrame:

df.team.value_counts()

A    3
B    2
C    1
Name: team, dtype: int64

Additional Resources

x