How to get the unique values of a Dataframe

0 min

In some cases, you might need to know what are the distinct values of a column in Python.

There exists an easy to do so using pandas DataFrame built-in unique() method.

Here is an example of retrieving the list of distinct products available in a DataFrame containing client orders.

import pandas as pd # We use the pandas library to create a dataframe

df = pd.DataFrame({"product": ["Socks", "Hat", "Cap", "T-Shirt", "Hat", "Socks"], "qty" : [1,1,2,1,3,4]})

print(df["product"].unique()) # We retrieve the product list.

Here you are !