DataFrame.query
df.query("expr") filters rows using a string expression where column names are variables: df.query("salary > 50000 and dept == 'Sales'"). It accepts and, or, not, in and comparison operators, and @name refers to a Python variable.
Syntax
df.query(expr, inplace=False, **kwargs)| Part | What it is |
|---|---|
expr | The condition as a string; column names are used directly |
inplace | If True, filter df in place instead of returning a new DataFrame |
Examples
import pandas as pd
df = pd.DataFrame({"name": ["Ana", "Ben", "Cara", "Dan"],
"dept": ["Sales", "IT", "Sales", "HR"],
"salary": [50000, 62000, 58000, 45000]})
df.query("salary > 50000 and dept == 'Sales'")Result: name dept salary 2 Cara Sales 58000
limit = 55000
df.query("salary >= @limit")Result: name dept salary 1 Ben IT 62000 2 Cara Sales 58000
@ pulls in a variable from the surrounding Python scope.
df.query("dept in ['IT', 'HR']")Result: name dept salary 1 Ben IT 62000 3 Dan HR 45000
Common mistakes
- UndefinedVariableError for a Python variable: prefix it with @ inside the expression (salary > @limit).
- Column names with spaces or that start with a digit raise a syntax error: wrap them in backticks.
- Comparing with a string that contains quotes: alternate quote types, for example df.query('name == "O\'Brien"') or build the mask with ==.
Practice DataFrame.query
These drills check your answer on live data, free, no account needed.
- Readable filters with queryUse `query` to find employees in Sales or Marketing who earn more than 60,000.
Questions
Is query faster than boolean indexing?
For large frames (over about 100k rows) query can be faster because it uses the numexpr engine when installed; for small frames the two are equivalent and boolean masks are easier to debug.
Can I use string methods in query?
Yes, call them as in Python: df.query("name.str.startswith('A')"). Add engine="python" if the default engine rejects the method.
Related
- Filter rowsHow to filter rows in a pandas DataFrame by one or several conditions using boolean masks, with the & | ~ operators, parentheses, isin, between and query.
- isinHow to use isin in pandas to keep rows whose value is in a list, set or Series, exclude with ~, check several columns at once and avoid the type mismatch.
- loc vs ilocThe difference between loc and iloc in pandas: loc selects by index label and column name, iloc by integer position. Slices, lists and boolean masks.
- str.containsHow to use str.contains in pandas to find rows containing text: case-insensitive matching, several keywords with regex, literal characters, and the NaN error.