🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.filter()

AI & DATA SCIENCE // df-filter

df.filter() selects a subset of a DataFrame's columns (or rows) based on their labels matching a name list, a substring, or a regex pattern — it filters by label, not by data values.

Syntax

df.filter(items=None, like=None, regex=None, axis=None)

Deep Dive Course

df.filter()'s name is a common source of confusion: unlike Python's built-in filter() or boolean indexing, which select rows based on their data values, df.filter() selects columns, by default, or rows based purely on their labels matching a criterion — items for an exact list of names, like for a substring contained in the label, or regex for a full pattern match. It's useful for quickly narrowing down to a group of similarly-named columns, like every column whose name starts with a common prefix.

1Understanding df.filter()

df.filter()'s name is a common source of confusion: unlike Python's built-in filter() or boolean indexing, which select rows based on their data values, df.filter() selects columns, by default, or rows based purely on their labels matching a criterion — items for an exact list of names, like for a substring contained in the label, or regex for a full pattern match. It's useful for quickly narrowing down to a group of similarly-named columns, like every column whose name starts with a common prefix.

💡

Don't confuse df.filter() with filtering rows by a data condition — despite the name, filter() selects columns, or rows, by their labels matching a pattern, not by any actual data values; use boolean indexing or query() for value-based filtering instead.

editor.html
import pandas as pd

df = pd.DataFrame({"sales_q1": [100], "sales_q2": [150], "region": ["East"]})
print(df.filter(like="sales"))
localhost:3000

2Practical Example

Here is a real-world application of df.filter() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"name": ["Alice"], "age": [30], "city": ["NYC"]})
print(df.filter(items=["name", "city"]))
localhost:3000

3Best Practices

Follow these guidelines when working with df.filter():

1. Use filter(like='prefix') or filter(regex=...) to quickly select a group of similarly-named columns, instead of manually listing them all out

2. Reach for boolean indexing or query() instead of filter() when the selection criterion is based on data values, not column/row names

3. Pass axis=0 explicitly when you want to filter rows by their index labels instead of the default column-label filtering

⚠️

Tip: Don't confuse df.filter() with filtering rows by a data condition — despite the name, filter() selects columns, or rows, by their labels matching a pattern, not by any actual data values; use boolean indexing or query() for value-based filtering instead.

editor.html
import pandas as pd

df = pd.DataFrame({"sales_q1": [100], "sales_q2": [150], "region": ["East"]})
print(df.filter(like="sales"))
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"sales_q1": [100], "sales_q2": [150], "region": ["East"]})
print(df.filter(like="sales"))
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"name": ["Alice"], "age": [30], "city": ["NYC"]})
print(df.filter(items=["name", "city"]))

Best Practices

  • Use filter(like='prefix') or filter(regex=...) to quickly select a group of similarly-named columns, instead of manually listing them all out
  • Reach for boolean indexing or query() instead of filter() when the selection criterion is based on data values, not column/row names
  • Pass axis=0 explicitly when you want to filter rows by their index labels instead of the default column-label filtering

Interview Question

Why would using df.filter() to select rows where a column's value exceeds some threshold not work as expected?

Hint: Think about what filter() is actually designed to match against.

filter() is specifically designed to select based on labels — column names by default, or row index labels with axis=0 — matching them against an exact list, a substring, or a regex pattern; it never looks at the DataFrame's actual data values at all. A condition like rows where a column exceeds some threshold is a data-based condition, not a label-based one, so it's not something filter() is built to express — that kind of filtering requires boolean indexing or df.query() instead, both of which evaluate the actual values stored in the DataFrame.

Exercises

MediumPractice using df.filter() in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"sales_q1": [100], "sales_q2": [150], "region": ["East"]})
print(df.filter(like="sales"))

Frequently Asked Questions

Why would using df.filter() to select rows where a column's value exceeds some threshold not work as expected?

filter() is specifically designed to select based on labels — column names by default, or row index labels with axis=0 — matching them against an exact list, a substring, or a regex pattern; it never looks at the DataFrame's actual data values at all. A condition like rows where a column exceeds some threshold is a data-based condition, not a label-based one, so it's not something filter() is built to express — that kind of filtering requires boolean indexing or df.query() instead, both of which evaluate the actual values stored in the DataFrame.

Related Functions

boolean-indexingdf-querydf-columns