The pandas query method filters a DataFrame by turning a string expression into a row-selection query. It offers a readable alternative to Boolean indexing: instead of repeating column brackets, write column names directly inside the expression. The result is a filtered set of rows, and the method cannot appear on the left side of an assignment. section 2.12
Filtering products with readable expressions
To select rows where v exceeds 300, use df.query('v > 300'). Combine conditions with &, |, and comparisons such as df.query('(v > 300) & (w % 2 == 1)'). Expressions can also compare a column with an aggregate, as in df.query('sales > sales.mean()'), after which ordinary column selection can keep only product_id and name. Follow-up tasks can select products in the top sales quantile, combine below-average sales with above-average wholesale prices, or filter IDs between 80 and 100. section 2.12 section 2.14
df.query('v > 300 & w % 2 == 1') #1
Compare the traditional Boolean-index form with query syntax, then add parentheses to make compound conditions easier to read. section 2.12
Use query alongside loc rather than treating it as a replacement for every selection task. Practice both forms on taxi-payment questions, product analysis, and increasingly selective parking-ticket queries. With larger DataFrames, query may run significantly faster and use less memory; on small DataFrames, it can be unnecessary and slower. Compare alternatives with timeit, including loc, query, eval, and isin. section 3.4 section 12.10
