Readable conditional filtering
Select rows using readable column expressions.
Pandas · Module
Filters a DataFrame using a boolean expression written as a string. The expression can use DataFrame columns and configurable evaluation context, parser, and engine settings.
DataFrame.query evaluates a string expression against a DataFrame's columns and returns the matching rows. Its parser and evaluation engine are configurable; external variables can be supplied through the evaluation context, and inplace=True updates the existing frame instead of returning a new one.
DataFrame.query(expr, *, parser='pandas', engine=None, local_dict=None, global_dict=None, resolvers=None, level=0, inplace=False)| Name | Type | Description |
|---|---|---|
expr | str | Query string to evaluate against the DataFrame. |
parser | {'pandas', 'python'}, default 'pandas' | Expression parser: 'pandas' or 'python'; defaults to 'pandas'. |
engine | {'python', 'numexpr'}, default None | Evaluation engine: 'python', 'numexpr', or None; defaults to None. |
local_dict | dict or None, optional | Mapping of local variables used while evaluating the expression. |
global_dict | dict or None, optional | Mapping of global variables used while evaluating the expression. |
resolvers | list-like, optional | Additional resolvers used during expression evaluation. |
level | int, default 0 | Stack level used to resolve variables. |
inplace | bool, default False | Whether to modify this DataFrame instead of creating a new one. |
DataFrame or None: the DataFrame produced by the query, or None when inplace=True.
import pandas as pd
orders = pd.DataFrame({
"customer": ["A", "B", "A"],
"amount": [120, 80, 200],
})
minimum = 100
selected = orders.query("amount >= @minimum")
print(selected)Filters rows with an expression, while @minimum refers to a Python variable outside the DataFrame.
Select rows using readable column expressions.
Reuse a threshold or other runtime value in a filter.
Apply the selection directly to an existing frame.
The method accepts a string expression and evaluates it against DataFrame columns, returning the filtered DataFrame. By default it uses the pandas parser and chooses an evaluation engine automatically.
Follow-up: How would you preserve strict Python expression semantics? Set parser='python' and, when needed, choose engine='python'.
Use @ before a name in the expression to refer to a variable from the surrounding evaluation environment, such as query('amount >= @minimum'). The expression itself remains a string.
Follow-up: How can the available external names be controlled? Provide local_dict or global_dict mappings to define the evaluation context.
The engine controls expression evaluation. With engine=None, pandas tries numexpr and falls back to Python; engine='numexpr' can accelerate complex expressions on large frames.
Follow-up: What happens when inplace=True? The matching rows are applied to the existing DataFrame and the return value is None.
pandas.DataFrame.evalpandas.eval