The 6 AI Engineering Patterns, come build with Greg live:Β Starts Jan 6th, 2025
Leverage
Pandas functions

Pandas Unique – pd.Series.unique()

Pandas unique values extraction

Have you ever wondered what the distinct (unique) values are within your pandas DataFrame column? That is where Pandas Unique comes into play.

pandas.Series.unique()

I have two main uses for finding distinct values:

  • When I’m trying to visually see what values lie within a pandas column. I will often go between .unique() and .value_counts() to get a feel for my data.
  • When I’m trying to iterate through a DataFrame. I will loop through the unique values of a column, then filter a DataFrame by it’s unique value. Check out the examples below for an instance of this.

Pseudo Code: Look at a Pandas Series, then return only the distinct values. No showing multiple values allowed.

Pandas Unique

.unique() Parameters

None! .unique() does not take any values. Simply pass your list of values or a series and the distinct values will be returned to you.

One thing to note: You can call pd.unique() and pass a list of values, or you can call pd.Series.unique() and get the distinct values right on your series.

Now the fun part, let’s take a look at a code sample

Link to code

Official Documentation

On this page