How to get the unique values of a Dataframe
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
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 !
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Related Articles
Continue your learning journey with these related topics
Master Data Science in Days, Not Months 🚀
Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚