Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 22

Filtering and Sorting Data

Recommended resources

Hello! Welcome to the next lesson in our journey with Pandas.

In our last session, we focused on the crucial skill of loading data from various file formats like CSV, Excel, and JSON into a DataFrame. Now that you can get data into your Python environment, the next logical step is to learn how to sift through it to find what's truly important.

This lesson is all about asking questions of your data. You'll learn how to filter your DataFrame to select only the rows that meet specific criteria, much like using a filter in a spreadsheet but with far more power and flexibility. We will also cover how to sort your data based on the values in one or more columns to bring the most relevant information to the top. These two skills—filtering and sorting—are the bedrock of data exploration and analysis.

1. The Core of Filtering: Boolean Indexing

Before we can filter, we need to understand the mechanism Pandas uses: boolean indexing. The idea is simple: you create a Series of True and False values that is the same length as your DataFrame. This is often called a "boolean mask". When you apply this mask to your DataFrame, Pandas returns only the rows where the mask's value is True.

Let's see this foundational concept in action.

Python Pandas Tutorial (Part 4): Filtering - Using Conditionals to Filter Rows and Columns

This first video from Corey Schafer's excellent Pandas series explains the concept of a boolean mask. It shows that when you write a comparison, Pandas doesn't immediately return the filtered data, but rather a Series of True/False values.

Watch from the beginning to 02:34. Pay close attention to the output when a comparison like df['last'] == 'Doe' is executed. Understanding this output is key to grasping how all filtering works in Pandas.

As the video explains, a condition like df['column'] == 'value' produces a boolean mask. Now, how do we use that mask to actually filter the DataFrame?

You can pass the mask directly inside square brackets (e.g., df[mask]), but the recommended and more powerful way is to use the .loc indexer, which you're already familiar with from a previous lesson.

df.loc[mask]

Using .loc is generally preferred because it's more explicit and avoids potential confusion. It also allows you to select specific columns for your filtered rows at the same time: df.loc[mask, ['column1', 'column2']].

The next video segment demonstrates exactly this.

Python Pandas Tutorial (Part 4): Filtering - Using Conditionals to Filter Rows and Columns

Now, let's see how to apply the boolean mask to our DataFrame using both direct indexing and the .loc indexer.

Watch from 02:34 to 06:18. Notice how both df[filt] and df.loc[filt] produce the same result, and see how .loc makes it easy to select specific columns after filtering.


2. Combining and Negating Conditions

Real-world analysis rarely involves a single condition. You'll often need to find data that meets multiple criteria (e.g., "sales greater than 1000 and in the 'Electronics' category") or one of several criteria (e.g., "country is 'USA' or 'Canada'").

Pandas uses bitwise operators for this, which might look a little different from standard Python's and and or.

  • & for AND (select rows where both conditions are True)
  • | for OR (select rows where either condition is True)
  • ~ for NOT (select rows where the condition is False; it inverts the mask)

Crucially, you must wrap each individual condition in parentheses () when combining them. This is a common source of errors for beginners.

df.loc[(condition1) & (condition2)]

Let's watch how to implement this.

Python Pandas Tutorial (Part 4): Filtering - Using Conditionals to Filter Rows and Columns

This video segment shows how to combine multiple conditions with & and |, and how to negate a filter with ~.

Watch from 06:18 to 09:36. Pay special attention to the syntax: the use of & and | operators and the mandatory parentheses around each condition.

For a supplementary text-based explanation, you can refer to the "Boolean Indexing" section of this article.

Boolean Indexing and Sorting in Pandas

This article from Canard Analytics provides clear, text-based examples of filtering with single and multiple conditions. It's a great reference to have open as you practice.

Read the sections "Boolean Indexing", "Single Conditional", and "Multiple Conditionals". This will reinforce the concepts from the video, especially the importance of parentheses.


3. More Convenient Filtering Methods

While & and | are powerful, Pandas provides some convenient methods for common filtering scenarios.

The .isin() Method

Instead of chaining multiple | conditions like df.loc[(df['Country'] == 'USA') | (df['Country'] == 'India') | ...], you can use the .isin() method to check if a column's value is present in a list of allowed values.

countries_to_find = ['United States', 'India', 'United Kingdom', 'Germany', 'Canada']
filt = df['Country'].isin(countries_to_find)
df.loc[filt]

The .str.contains() Method

For string columns, you often need to find rows containing a certain substring. The .str.contains() method is perfect for this. It's particularly useful for "messy" text data.

# Find all rows where the 'LanguagesWorkedWith' column contains the word 'Python'
filt = df['LanguageWorkedWith'].str.contains('Python')
df.loc[filt]

Note the .str accessor before .contains(). This is how you access string-processing methods for a Series in Pandas.

Let's watch a practical demonstration of both of these methods on a large, real-world dataset.

Python Pandas Tutorial (Part 4): Filtering - Using Conditionals to Filter Rows and Columns

Corey Schafer now applies these filtering techniques to a larger Stack Overflow survey dataset, demonstrating the practical use of .isin() and .str.contains().

Watch from 09:36 to 19:16. Observe how .isin() simplifies filtering for multiple countries. For .str.contains(), notice the na=False argument, which is important for handling missing values (NaN) in text columns.

Test your understanding!

Imagine you have a DataFrame products_df like this:

import pandas as pd
data = {'product_name': ['Laptop', 'Keyboard', 'Monitor', 'Mouse', 'Webcam', 'Docking Station'],
        'category': ['Electronics', 'Electronics', 'Electronics', 'Electronics', 'Peripherals', 'Peripherals'],
        'price': [1200, 75, 300, 25, 50, 150],
        'stock': [8, 35, 12, 50, 20, 5]}
products_df = pd.DataFrame(data)

Write a single line of code to filter this DataFrame to find all products that are in the 'Electronics' category AND have a price less than $100.

Show answer
electronics_under_100 = products_df.loc[(products_df['category'] == 'Electronics') & (products_df['price'] < 100)]
print(electronics_under_100)

The output should be:

  product_name     category  price  stock
1     Keyboard  Electronics     75     35
3        Mouse  Electronics     25     50

4. Sorting Data by Column Values

Once you've filtered your data, you'll often want to sort it to make it easier to interpret. For example, you might want to see the most expensive products or the oldest records first. Pandas makes this straightforward with the .sort_values() method.

Pandas Sorting Illustration
Sorting allows you to reorder the rows of your DataFrame based on the values in one or more columns.

You can sort by a single column or by multiple columns in a specific order.

Complete Python Pandas Data Science Tutorial! (Reading CSV/Excel files, Sorting, Filtering, Groupby)

This short video by Keith Galli provides a very quick and effective demonstration of how to use .sort_values() for both single and multiple columns.

Watch from 16:03 to 18:19. Pay attention to the by parameter, which takes the column name(s), and the ascending parameter, which controls the sort order (True for A-Z, smallest to largest; False for Z-A, largest to smallest).

As you saw in the video, the key parameters are:

  • by: A string for a single column df.sort_values(by='price') or a list of strings for multiple columns df.sort_values(by=['category', 'price']).
  • ascending: A single boolean (True or False) or a list of booleans corresponding to the columns in by. For example, ascending=[True, False] would sort the first column A-Z and the second column Z-A.

5. An Important Final Step: Resetting the Index

When you filter a DataFrame, the original index values are kept. This can be confusing. Look at the index of the electronics_under_100 DataFrame from our exercise above: it's 1 and 3, not 0 and 1. This can cause problems in later processing steps.

It is good practice to reset the index after filtering to create a clean, new 0, 1, 2, ... index. You can do this with the .reset_index() method.

Complete Python Pandas Data Science Tutorial! (Reading CSV/Excel files, Sorting, Filtering, Groupby)

Let's look at why and how to reset the index after a filtering operation. This is a small but very important practical tip.

Watch from 35:45 to 37:45. Note the use of drop=True to avoid creating a new column with the old index values, and inplace=True to modify the DataFrame directly without reassigning it.

So, a complete filtering and cleaning operation often looks like this:

# Filter for electronics under $100
filtered_df = products_df.loc[(products_df['category'] == 'Electronics') & (products_df['price'] < 100)]

# Sort by price, descending
sorted_df = filtered_df.sort_values(by='price', ascending=False)

# Reset the index
final_df = sorted_df.reset_index(drop=True)

print(final_df)

This can also be chained together into a single, elegant line of code:

final_df = (products_df
            .loc[(products_df['category'] == 'Electronics') & (products_df['price'] < 100)]
            .sort_values(by='price', ascending=False)
            .reset_index(drop=True)
           )

Conclusion

You now have two of the most fundamental data manipulation tools in your Pandas toolkit. Being able to effectively filter and sort your data is the first step toward uncovering insights and preparing your dataset for machine learning.

Key Takeaways:

  • Filtering in Pandas works by creating a boolean mask (a Series of True/False values).
  • Apply masks using .loc for clarity and power: df.loc[mask].
  • Combine conditions using & (AND) and | (OR), remembering to wrap each condition in parentheses.
  • Use the ~ operator to negate a condition.
  • Leverage convenience methods like .isin() for multiple OR conditions and .str.contains() for substring searches.
  • Use .sort_values() to reorder your DataFrame by one or more columns, controlling the direction with ascending.
  • Always consider using .reset_index(drop=True) after filtering to create a clean new index.

Preview of the Next Lesson:
Filtering and sorting help you select and view the data you want. But what if you need to change the data itself? In our next lesson, we will explore how to transform data by applying functions and creating new columns. This will allow you to clean data, calculate new metrics, and engineer features—essential steps on the path to building machine learning models.