Run pandas Online

Filter DataFrame Rows in pandas

Keep only the rows you need with a condition, combine conditions, or use query() for readable filters.

  • Intermediate
  • 2 min read
  • 1 runnable example
What you'll learn
  • Boolean masks on a column
  • Combining conditions with & and |
  • df.query() and isin()
students[students["score"] >= 90]
Ada 95True → kept
Lin 88False → dropped
Sam 91True → kept

A 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.

From a highlighter to code
students["score"] >= 90🖍️ Highlight rows with 90 or more
[True, False, True, False]📌 Which rows are highlighted
students[...]📠 Copy only the highlighted rows
high_scores📋 Ada and Sam, with their original row labels
A True/False column selects rows.
In one sentence

Filtering a DataFrame uses a boolean Series, one True or False per row, to keep only the rows where the condition is True.

Key points

  1. students["score"] >= 90 gives True or False for each row.
  2. students[...] with that condition keeps only the True rows.
  3. high_scores keeps the original row labels 0 and 2.
  4. students itself 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 groupby instead of filtering each group by hand.

Example code

Run 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)
Get a Run button for this example

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.

  1. Add rows, columns, and missing values.
  2. Combine filtering, sorting, and groupby into a small report.
  3. Load a real file, inspect dtypes, and handle invalid data.
Run in the Python compiler →

Common mistakes

Combining conditions with and

and does not work on Series; use & with parentheses around each condition.

Problem
students[students["score"] >= 80 and students["score"] < 90]
Fix
students[(students["score"] >= 80) & (students["score"] < 90)]

Finished this lesson?

Mark it as learned to track your progress.

Next: pandas GroupBy →

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