Sample DataFrame context
A sales table where you want compact filter expressions.
Syntax or pattern
df.query("revenue > 1000 and region == 'East'")5 practical examples
Filter high-value orders
Keep rows where revenue is above a threshold.
high_value = sales[sales["revenue"] > 1000]The result contains only larger orders.
Filter by multiple conditions
Keep East region orders above 500.
filtered = sales[(sales["region"] == "East") & (sales["revenue"] > 500)]Both conditions must be true.
Filter by a list
Keep selected regions.
selected = sales[sales["region"].isin(["East", "West"])]The output contains only rows with those region values.
Filter text values
Find products containing a word.
matches = sales[sales["product"].str.contains("pro", case=False, na=False)]This is useful for searching messy product names.
Select columns after filtering
Return only the fields needed for a report.
report = filtered[["order_id", "region", "product", "revenue"]]The filtered output is easier to review or export.
Common mistakes to avoid
- Changing the original DataFrame before checking the result.
- Forgetting to inspect row counts before and after the operation.
- Using a pattern without confirming column names and data types.
FAQ
What is the main use of query Examples?
Use DataFrame.query for readable filters with numbers, text, dates and variables.
Should I use this in a notebook or a script?
Both work. Use a notebook while exploring the data, then move the final workflow into a repeatable script when it is stable.
How do I avoid breaking my source data?
Create a copy of the DataFrame before transforming it and check row counts, missing values and sample rows after each important step.