Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to get unique values in a column Pandas

Understanding Unique Values in Pandas

When you're working with data in Python, one of the most common tasks you'll encounter is finding unique values within a column of a dataset. This is where Pandas, a powerful data manipulation library, comes in handy. Imagine you have a basket of fruits with apples, bananas, and oranges, and you want to know what different kinds of fruits are there without counting duplicates. In the world of data analysis, this is akin to extracting unique values from a column.

Setting Up Your Environment

Before diving into the process of finding unique values, you need to set up your environment. You'll need Python installed on your computer, and you'll need to install Pandas if you haven't already. You can install Pandas using pip, which is the package installer for Python:

pip install pandas

Once Pandas is installed, you can import it into your Python script or notebook:

import pandas as pd

Creating a DataFrame

To work with data in Pandas, we use something called a DataFrame. Think of a DataFrame as a table, much like one you'd see in a spreadsheet. It has rows and columns, and each column can contain data of various types, such as numbers, strings, or dates.

Let's create a simple DataFrame to work with:

# Sample data for our DataFrame
data = {
    'Fruit': ['Apple', 'Banana', 'Orange', 'Apple', 'Banana', 'Apple'],
    'Quantity': [5, 3, 8, 6, 7, 8]

# Creating a DataFrame from the data
df = pd.DataFrame(data)

# Display the DataFrame

The output will be a table with a 'Fruit' column and a 'Quantity' column, showing the respective quantities for each fruit entry.

Extracting Unique Values

To find unique values in the 'Fruit' column, we use the .unique() method. This method will go through the column, pick out all the unique entries, and ignore any repeats.

Here's how you do it:

unique_fruits = df['Fruit'].unique()

The output will be an array of unique fruits:

['Apple', 'Banana', 'Orange']

Understanding the Output

The .unique() method returns a NumPy array of unique values. NumPy is another library in Python that's used for numerical computations. An array is like a list, but it's specifically designed to handle numerical data efficiently.

Counting Unique Values

Sometimes, you might want to know not just which values are unique, but also how many unique values there are. In that case, you can use the .nunique() method, which stands for "number of unique values."

Here's how you use it:

number_of_unique_fruits = df['Fruit'].nunique()

The output tells us how many different fruits are in our dataset:


Dealing with Missing Values

Data isn't always perfect. Sometimes, you'll have missing values, which are represented as NaN (Not a Number) in Pandas. If you're counting or listing unique values, you might want to exclude these.

Here's an example of how to handle missing values:

# Adding a row with a missing value
df.loc[6] = [None, 9]

# Getting unique fruits excluding NaN
unique_fruits_excluding_NaN = df['Fruit'].dropna().unique()

This will give you the same array of unique fruits as before, without counting the None value.

Applying Unique Values in a Real-World Scenario

Now, let's apply what we've learned to a more realistic dataset. Imagine you have a dataset of customers and their purchases. You want to find out how many different items have been bought.

First, you'd import your dataset into a DataFrame. Then, you'd use the .unique() method on the item column just as we did with the fruit example.

# Assuming 'items' is the column with the purchased items
unique_items = purchases_df['items'].unique()

This would give you a list of all the unique items purchased by customers.

Visualizing Unique Values

Visualizing data can often give you more intuition about it. You can use a bar chart to represent the frequency of each unique value. Here's how you can do it using Pandas:

# Count the occurrences of each fruit
fruit_counts = df['Fruit'].value_counts()

# Plot a bar chart

This code will produce a bar chart showing how many times each fruit appears in your dataset.

Conclusion: The Power of Simplicity

In this journey through the orchards of data with Pandas, we've seen how simple methods like .unique() and .nunique() can provide us with valuable insights. They allow us to identify the diversity within our data, much like distinguishing between different fruits in a basket.

As you continue to explore the world of data with Python and Pandas, remember that these tools are designed to make your life easier. With just a few lines of code, you can uncover patterns and information that would be time-consuming to find manually. So, embrace the simplicity, and let Pandas handle the complexity of your data.

As you grow more comfortable with these methods, you'll find that they serve as building blocks for more advanced data analysis techniques. Just as every fruit in your basket adds flavor to a fruit salad, every unique value in your dataset adds depth to your analysis. Keep experimenting, keep learning, and enjoy the fruits of your labor!