Filter DataFrame Rows in pandas
Keep only the rows you need with a condition, combine conditions, or use query() for readable filters.
- Boolean masks on a column
- Combining conditions with & and |
- df.query() and isin()
Ada 95True → keptLin 88False → droppedSam 91True → keptA True/False column picks the rows
The problem
Analysis usually starts by narrowing down: only this year's orders, only students scoring 90 or more, only failed payments. Looping over rows to find them is slow and verbose; pandas filters a whole table with one condition.
Think of it like this
Filtering rows is like a teacher running a highlighter down the score column, marking every row of 90 or more, and then photocopying only the highlighted rows.
students["score"] >= 90🖍️ Highlight rows with 90 or more[True, False, True, False]📌 Which rows are highlightedstudents[...]📠 Copy only the highlighted rowshigh_scores📋 Ada and Sam, with their original row labelsFiltering a DataFrame uses a boolean Series, one True or False per row, to keep only the rows where the condition is True.
Key points
students["score"] >= 90gives True or False for each row.students[...]with that condition keeps only the True rows.high_scoreskeeps the original row labels 0 and 2.studentsitself is not changed.
When should you use it?
- Selecting the rows that matter before counting, averaging or plotting.
- Finding problems: missing values, negative amounts, failed tests.
- Splitting data into groups for comparison.
When not to use it
- Very complex rules: compute a named mask step by step, or use
query()for readability. - Aggregating per group: use
groupbyinstead of filtering each group by hand.
Example code
import pandas as pd
students = pd.DataFrame({
"name": ["Ada", "Lin", "Sam", "Mia"],
"score": [95, 78, 91, 84],
})
high_scores = students[students["score"] >= 90]
print(high_scores)
Result of running the code
name score
0 Ada 95
2 Sam 91
Code explanation
The condition produces a boolean Series aligned with the DataFrame index. Passing it inside brackets returns matching rows.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Add rows, columns, and missing values.
- Combine filtering, sorting, and groupby into a small report.
- Load a real file, inspect dtypes, and handle invalid data.
Common mistakes
Combining conditions with and
and does not work on Series; use & with parentheses around each condition.
students[students["score"] >= 80 and students["score"] < 90]
students[(students["score"] >= 80) & (students["score"] < 90)]
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
All challenges