Skip to main content

How to Filter the Rows of a pandas DataFrame with a Condition

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Most questions about a table start with "which rows": the warm cities, the big orders, the hours before a date. pandas answers them by putting a condition inside the table's square brackets, df[df["temp"] > 20], and keeping the rows where it's true. This guide filters a small table with pandas 3.1.2 on Python 3.15.2.

Syntax​

Syntax
<rows> = <table>[<table>["<column>"] <comparison> <value>]
PartWhat it means
<table>["<column>"] <comparison> <value>A condition such as cities["temp"] > 20. It gives one True or False per row
<table>[...] around itKeeps the rows where the condition is True
<comparison>>, >=, <, <=, == (equal) or != (different)

The indexing guide calls this boolean indexing: "Another common operation is the use of boolean vectors to filter the data."

The Example​

A table holds four cities and today's temperature in each. The script keeps the cities warmer than 20 degrees.

Prerequisites​

  • pandas in your project, for example with uv add pandas. This guide uses pandas 3.1.2 on Python 3.15.2.

Step 1: Write the Script​

filter_rows.py
import pandas as pd

cities = pd.DataFrame({"city": ["Oslo", "Rome", "Cairo", "Lima"], "temp": [4, 18, 29, 21]})

warm = cities[cities["temp"] > 20]
print(warm)

The filter_rows.py script in a macOS-style code window: a cities DataFrame with city and temp, warm = cities[cities["temp"] > 20] and print(warm).

Step 2: Run It​

Terminal
uv run python filter_rows.py
Output
city temp
2 Cairo 29
3 Lima 21

The terminal after uv run python filter_rows.py: two rows, Cairo 29 with label 2 and Lima 21 with label 3.

Cairo and Lima are above 20. Each row kept its original label, 2 and 3, so you can still tell where it came from. The command works unchanged in PowerShell.

What the Condition Gives​

On its own, cities["temp"] > 20 is a column of True and False, one per row:

Output
0 False
1 False
2 True
3 True
Name: temp, dtype: bool

Putting it inside cities[...] keeps the rows marked True.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting the rows to be numbered from 0 againThe filtered rows keep their old labels, 2 and 3Add .reset_index(drop=True)
Using = instead of == for "equal"SyntaxError: cannot assign to subscript here. Maybe you meant '==' instead of '='?Write cities["city"] == "Rome"

Troubleshooting​

SymptomLikely causeFix
Empty DataFrame, with Columns: [city, temp] and Index: []No row meets the condition: temp > 40 gave thisPrint the condition alone to see each row's True or False

FAQ​

How do I number the filtered rows from 0?​

With reset_index(drop=True). cities[cities["temp"] > 20].reset_index(drop=True) printed Cairo at 0 and Lima at 1. drop=True throws away the old labels instead of keeping them as a column.

How do I filter on two conditions?​

Join them with & for "and" or | for "or", each in its own brackets: (cities["temp"] > 20) & (cities["city"] != "Lima").

Conclusion​

cities[cities["temp"] > 20] kept Cairo and Lima, the two rows where the condition was True, with their original labels. Print the condition by itself to see why a row stays or goes.

Additional Resources​