Pandas – Mastering DataFrame.filter() method (5 examples)

Updated: February 20, 2024 By: Guest Contributor Post a comment


In the world of data analysis with Python, the Pandas library stands out for its powerful and flexible data structures. One particularly useful tool at our disposal is the DataFrame.filter() method. This method allows for slicing and dicing data in a DataFrame based on specific criteria, making it a staple in data preprocessing and exploration. In this tutorial, we’ll journey through mastering the filter() method with 5 detailed examples, evolving from basic to advanced usage. By the end, you’ll have a thorough understanding of how to leverage this method to sift through your data effectively.

Getting Started with DataFrame.filter()

Generating a Sample DataFrame to Work with

Before diving into the examples, let’s establish our environment. Ensure you have Pandas installed, and import it along with the necessary data:

import pandas as pd

# Sample DataFrame
cars = {'Brand': ['Honda', 'Toyota', 'Ford', 'Audi'],
        'Year': [2012, 2014, 2011, 2015],
        'Price': [22000, 24000, 27000, 35000]}
df = pd.DataFrame(cars)

Example 1: Filtering Columns by Name

The most basic use of the filter() method is to select columns by their names. For instance, if you want to view only the ‘Brand’ and ‘Price’ columns, you can achieve this as follows:

print(df.filter(items=['Brand', 'Price']))

This will output:

    Brand  Price
0  Honda  22000
1  Toyota  24000
2   Ford  27000
3    Audi  35000

Example 2: Filtering Columns by Regex

Another powerful feature of the filter() method is the ability to select columns based on regular expressions (regex). This is especially useful for datasets with a large number of similarly named columns. For instance, if you want to select all columns that contain the word ‘Year’, you might use:


This will display the ‘Year’ column:

0  2012
1  2014
2  2011
3  2015

Example 3: Filtering Rows Using Axis Parameter

While the filter() method is typically understood as a tool for column selection, it also provides the capability to filter rows. This can be done by utilizing the axis parameter. For example, to filter rows where the index is within a specific range:

print(df.filter(like='2', axis=0))

This will result in:

   Brand  Year  Price
2   Ford  2011  27000

Example 4: Using filter() with Custom Functions

Advancing our exploration, let’s see how filter() can be combined with custom functions to further refine our data selection. This is particularly useful in complex data preparation tasks. For instance, you can filter columns based on a custom criterion such as all columns where the average value is greater than a certain threshold:

def custom_filter(col):
    return col.mean() > 24000

filtered_columns = df.select_dtypes(include=['number']).apply(custom_filter)

This method might produce output such as:

0  22000
1  24000
2  27000
3  35000

Example 5: Integrating filter() in Data Processing Pipelines

As a final example, let’s delve into integrating the filter() method into complex data processing pipelines. This involves using filter() in conjunction with other Pandas methods to perform detailed and targeted data analysis. Suppose you’re working with a large dataset with numerous columns but are only interested in columns that adhere to a set of criteria defined by multiple regex patterns. Here’s how you could set up such a pipeline:

patterns = '|'.join(['Honda', '201.*'])
filtered_df = df.filter(regex=patterns)

This nuanced approach allows you to efficiently comb through vast amounts of data, focusing only on relevant pieces. It showcases the true power and flexibility of the filter() method when used thoughtfully within data analysis workflows.


In wrapping up, the DataFrame.filter() method is an invaluable tool in the data analyst’s arsenal, offering both simplicity and powerful customization for data selection and preprocessing tasks. Through the examples provided, you’ve seen its versatility across a range of use cases from basic column filtering to integrating it in advanced data processing pipelines. Mastery of this method enriches your data manipulation capabilities, paving the way for deeper data insights.