Filtering
Pandas Basics
1 min read
This section is 1 min read, full guide is 30 min read
Published Sep 29 2025, updated Oct 24 2025
21
Show sections list
0
Log in to enable the "Like" button
0
Guide comments
0
Log in to enable the "Save" button
Respond to this guide
Guide Sections
Guide Comments
PandasPython
Filtering means selecting rows based on conditions applied to columns.
Sample data:
import pandas as pd
df = pd.DataFrame({
"Name": ["Alice", "Bob", "Charlie", "David"],
"Age": [25, 30, 35, 40],
"Salary": [50000, 60000, 75000, 80000]
})
Copy to Clipboard
Single Condition
Using the boolean indexing, so what its actually passing in from the condition is the array of True/False values and only displaying the ones where the value equates to True.
# Filter rows where Age > 30
filtered_df = df[df["Age"] > 30]
print(filtered_df)
Copy to Clipboard
Toggle show comments
Output:
Name Age Salary
2 Charlie 35 75000
3 David 40 80000
Copy to Clipboard
Multiple Conditions (AND / OR)
- AND:
& - OR:
| - Brackets required around each condition
# Age > 30 AND Salary > 70000
df[(df["Age"] > 30) & (df["Salary"] > 70000)]
# Age < 35 OR Salary < 60000
df[(df["Age"] < 35) | (df["Salary"] < 60000)]
Copy to Clipboard
Toggle show comments
Using .query()
- Cleaner syntax for filtering with column names directly.
df.query("Age > 30 & Salary > 70000")
Copy to Clipboard
Explanation:
- Useful when you have multiple conditions.
- You can use brackets to make more readable.
- Use
&for AND,|for OR,~for NOT. Department == 'IT': Department is IT.Salary <= @max_salary: You can pass Python variables using the@symbol, in this example the variable is max_salary.- Column names must be valid Python identifiers (no spaces, no punctuation).
- Workaround: rename columns or use backticks:
df.query("`Column Name` > 10")
- Workaround: rename columns or use backticks:
- Only for row filtering, not for column selection.
Filtering with .isin()
- Select rows where a column value is in a list of values.
df[df["Name"].isin(["Alice", "David"])]
Copy to Clipboard
Output:
Name Age Salary
0 Alice 25 50000
3 David 40 80000
Copy to Clipboard
Filtering with string methods
# Names starting with 'C'
df[df["Name"].str.startswith("C")]
# Names containing 'a' (case-sensitive)
df[df["Name"].str.contains("a")]
Copy to Clipboard
Toggle show comments
Resetting Index after Filtering
After filtering or sorting, the index may be non-sequential:
filtered_df = df[df["Age"] > 30].reset_index(drop=True)
Copy to Clipboard
drop=True removes the old index.














