How to Filter the Rows of a pandas DataFrame with a Condition
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
<rows> = <table>[<table>["<column>"] <comparison> <value>]
| Part | What it means |
|---|---|
<table>["<column>"] <comparison> <value> | A condition such as cities["temp"] > 20. It gives one True or False per row |
<table>[...] around it | Keeps 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
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).](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/programming/python/2026/pandas-filter-rows-01-code.webp)
Step 2: Run It
uv run python filter_rows.py
city temp
2 Cairo 29
3 Lima 21

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:
0 False
1 False
2 True
3 True
Name: temp, dtype: bool
Putting it inside cities[...] keeps the rows marked True.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Expecting the rows to be numbered from 0 again | The filtered rows keep their old labels, 2 and 3 | Add .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
| Symptom | Likely cause | Fix |
|---|---|---|
Empty DataFrame, with Columns: [city, temp] and Index: [] | No row meets the condition: temp > 40 gave this | Print 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
- Boolean indexing — conditions,
&,|and~. - DataFrame.reset_index — numbering rows from 0 again.
