I think using functions here is unnecessary. It is better and mainly faster to use boolean indexing:

m = (df['Name'] == 'Alisa') & (df['Age'] > 24)
print(m)
0      True
1     False
2     False
3     False
4     False
5     False
6      True
7     False
8     False
9     False
10    False
11    False
dtype: bool

#invert mask by ~
df1 = df[~m]

For more complicated filtering, you could use a function which must return a boolean value:

def filter_fn(row):
    if row['Name'] == 'Alisa' and row['Age'] > 24:
        return False
    else:
        return True

df = pd.DataFrame(d, columns=['Name', 'Age', 'Score'])
m = df.apply(filter_fn, axis=1)
print(m)
0     False
1      True
2      True
3      True
4      True
5      True
6     False
7      True
8      True
9      True
10     True
11     True
dtype: bool

df1 = df[m]
Answer from jezrael on Stack Overflow
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.filter.html
pandas.DataFrame.filter — pandas 3.0.6 documentation
>>> # select columns by regular expression >>> df.filter(regex="e$", axis=1) one three mouse 1 3 rabbit 4 6 · >>> # select rows containing 'bbi' >>> df.filter(like="bbi", axis=0) one two three rabbit 4 5 6
Discussions

python - pandas: complex filter on rows of DataFrame - Stack Overflow
I would like to filter rows by a function of each row, e.g. Copydef f(row): return sin(row['velocity'])/np.prod(['masses']) > 5 df = pandas.DataFrame(...) filtered = df[apply_to_all_rows(df, f)] More on stackoverflow.com
🌐 stackoverflow.com
Python, Pandas: Filter rows of data frame based on function - Stack Overflow
I'm trying to filter a python data frame based on a sub string in one of the columns. If the number at position 13&14 of the ID field is 9, I want to... More on stackoverflow.com
🌐 stackoverflow.com
October 15, 2017
Pandas how do I 'apply' and filter at the same time?
Why not just include a tertiary operator inside your apply? t = t.applymap(lambda x: x * factor if x not in (-1, 'ERROR') else x) This will return a dataframe with all values other than -1 or 'ERROR' scaled and those two values unchanged. You can do the same thing with apply if you just want to operate on a single column and return a series. You'll just need to specify axis=1. More on reddit.com
🌐 r/learnpython
3
1
May 26, 2021
Polars: How to filter columns by date range?
Your gt than lt strategy is correct. But you must use chrono datetime literals. The datetime you see are from the python std lib. More on reddit.com
🌐 r/rust
4
2
March 27, 2023
🌐
ListenData
listendata.com › home › pandas
Python : 10 Ways to Filter Pandas DataFrame
It returns 4166 rows. ... In pandas package, there are multiple ways to perform filtering. The above code can also be written like the code shown below. This method is elegant and more readable and you don't need to mention dataframe name everytime when you specify columns (variables). newdf = df.query('origin == "JFK" & carrier == "B6"') How to pass variables in query function...
🌐
Python Examples
pythonexamples.org › pandas-dataframe-filter-rows
Pandas DataFrame - Filter Rows
To filter rows of Pandas DataFrame, you can use DataFrame.isin() function or DataFrame.query(). isin() can be used to filter the DataFrame rows based on the exact match of the column values or being in a range. query() can be used with a boolean expression, where you can filter the rows based ...
🌐
Built In
builtin.com › data-science › pandas-filter
How to Filter Pandas DataFrames | Built In
Filtering in Pandas means to subset (or display) certain rows and columns in a Pandas DataFrame based on specified conditions. The dataframe.filter() function is one method of filtering a DataFrame in Pandas. Lines, or rows, in a Pandas DataFrame ...
🌐
w3resource
w3resource.com › pandas › dataframe › dataframe-filter.php
Pandas DataFrame: - filter() function - w3resource
The filter() function is used to subset rows or columns of dataframe according to labels in the specified index. Note that this routine does not filter a dataframe on its contents.
Find elsewhere
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas filter rows by conditions
Pandas Filter Rows by Conditions - Spark By {Examples}
June 4, 2025 - You can filter the rows from Pandas DataFrame based on a single condition or multiple conditions using either loc[], query(), or apply() function. In this
🌐
HubSpot
blog.hubspot.com › home › the hubspot website blog
The HubSpot Website Blog
October 3, 2022 - HubSpot’s Website Blog covers everything you need to know to build maintain your company’s website.
🌐
Vultr Docs
docs.vultr.com › python › third-party › pandas › DataFrame › filter
Python Pandas DataFrame filter() - Filter Data Rows | Vultr Docs
December 25, 2024 - To filter rows based on conditions, consider combining filter() with boolean indexing or other functions like query(). ... This code filters the DataFrame to only include rows where the age is greater than 19.
🌐
Pandas
pandas.pydata.org › docs › getting_started › intro_tutorials › 03_subset_data.html
How do I select a subset of a DataFrame? — pandas 3.0.6 documentation
The notna() conditional function returns a True for each row the values are not a Null value. As such, this can be combined with the selection brackets [] to filter the data table.
🌐
Towards Data Science
towardsdatascience.com › home › latest › filtering pandas dataframe by objects’ content
Filtering Pandas Dataframe by objects' content | Towards Data Science
March 5, 2025 - The apply function iterates over rows (determined by the axis=1 parameter), represented by Series object, and maps a unary function to each. Essentially what a map function would do on a list.
🌐
W3Schools
w3schools.com › python › pandas › ref_df_filter.asp
Pandas DataFrame filter() Method
import pandas as pd data = { "name": ... "age"]) Try it Yourself » · The filter() method filters the DataFrame, and returns only the rows or columns that are specified in the filter....
🌐
Medium
medium.com › @epythonlab › top-10-pandas-functions-to-filter-data-like-a-pro-step-by-step-guide-536ac28cac7a
Top 10 Pandas Functions to Filter Data Like a Pro (Step-by-Step Guide) | by Epython Lab | Medium
September 10, 2024 - In this example, iloc[:2, :2] select the first two rows and the first two columns of the DataFrame. It’s essentially like slicing a list in Python. Speed: Filtering by index positions can be faster, especially with large datasets. Simple: Easy to apply when you only care about positions, not labels. ... The query() function allows you to filter your data using SQL-like syntax, which is more natural if you have experience with databases.
🌐
GeeksforGeeks
geeksforgeeks.org › python-pandas-dataframe-filter
Python | Pandas dataframe.filter() | GeeksforGeeks
November 19, 2018 - Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.filter() function is used to Subset rows or columns of dataframe according to labels in the specified index.
🌐
Towards Data Science
towardsdatascience.com › home › latest › data filtering in pandas
Data filtering in Pandas | Towards Data Science
March 5, 2025 - In addition, Pandas also allows you to obtain a subset of data based on column types and to filter rows with boolean indexing. In this article, we will cover the most common operations for selecting a subset of data from a Pandas data frame: (1) selecting a single column by label, (2) selecting multiple columns by label, (3) selecting columns by data type, (4) selecting a single row by label, (5) selecting multiple rows by label, (6) selecting a single row by position, (7) selecting multiple rows by position, (8) selecting rows and columns simultaneously, (9) selecting a scalar value, and (10) selecting rows using Boolean selection.
🌐
Deepnote
deepnote.com › blog › filter-with-pandas
Tutorial: filtering with Pandas
November 11, 2022 - Or we can return all rows corresponding ... we can filter rows in pandas data structures by passing in a list of Boolean values that correspond one to one with the indexes ......