Pandas DataFrame filter() Method

import pandas as pd 

df = pd.DataFrame({'MATH_SCORE': [85, 90], 'ENG_SCORE': [78, 88], 'AGE': [20, 21]})
print(df.filter(like='SCORE', axis=1))

Expected Output:

   MATH_SCORE  ENG_SCORE
0          85         78
1          90         88

1. Overview and Syntax

The DataFrame.filter() method subsets DataFrame rows or columns based on label matching rules. Unlike boolean indexing, filter() selects structures strictly by index or column names using explicit item lists, substring searching, or regular expressions.

Method Signature: DataFrame.filter(items=None, like=None, regex=None, axis=None)
Parameters:
• items: List of exact label names to keep.
• like: Substring contained in target labels.
• regex: Regular expression pattern to match labels.
• axis: 0 or 'index' (filter rows), 1 or 'columns' (filter columns, default).
Returns: A new DataFrame containing only matching columns or rows.

2. Subset Selection Using `items` and `like`

Select specific columns by exact name list using items or query columns matching partial text using like.

import pandas as pd 

df_sales = pd.DataFrame({
    'Q1_2025': [100, 150],
    'Q2_2025': [200, 250],
    'ANNUAL_TARGET': [500, 600]
})

# Filter columns containing '2025'
df_quarterly = df_sales.filter(like='2025')
print(df_quarterly)

3. Regular Expression Pattern Matching

Pass regex patterns into regex to perform complex pattern filtering across column names.

import pandas as pd 

df_data = pd.DataFrame({
    'cat_a': [1, 2],
    'cat_b': [3, 4],
    'dog_a': [5, 6]
})

# Match columns starting with 'cat'
df_cats = df_data.filter(regex='^cat')
print(df_cats)

4. Frequently Asked Questions (FAQs)

Q: Does filter() filter DataFrame rows by data values?
No, filter() operates exclusively on index and column labels. To filter rows based on data values, use boolean indexing (e.g., df[df['col'] > 5]) or query().

Q: Can I combine items, like, and regex together?
No, these arguments are mutually exclusive. Specify only one of items, like, or regex per call.

5. Common Errors & Troubleshooting

TypeError: Cannot pass more than one of (items, like, regex)
Cause: Supplying multiple matching arguments simultaneously.
Fix: Use only one selection parameter per filter() call.
Interactive Companion Jupyter Notebook

Explore interactive code cells, dynamically generate classes, test type vs isinstance comparisons, and run self-validating assertions.

View on GitHub Open In Colab

6. Related Tutorials




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer