Filter pandas DataFrame Rows
Boolean conditions make it possible to select only rows that match numeric, text, date, or combined criteria.
What is Filter pandas DataFrame Rows?
Boolean conditions make it possible to select only rows that match numeric, text, date, or combined criteria.
Filter DataFrame rows with one or more pandas conditions.
When should you use it?
- Read, clean, and transform tabular data.
- Analyze data from CSV, Excel, APIs, or databases.
- Prepare reports, statistics, and chart data.
Example code
main.py
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)
Expected output
name score
0 Ada 95
2 Sam 91
How it works
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.