Module 2: Selecting and filtering · Lesson 2.6 · about 15 min
query, isin and between
Three shortcuts that make filters shorter and easier to read: isin for "one of these values", between for ranges, and query for writing conditions like a sentence.
Module 2 · about 15 minutes · uses Lesson 2.3. You’ll be able to write common filters in fewer, clearer characters.
In everyday terms: a guest list (isin), an age limit (between), and a written house rule (query): three ways to decide who gets in.
1. isin: one of these
isin also takes another column or Series: “orders from customers in this list” is orders[orders['customer_id'].isin(vip['customer_id'])].
2. between: a range
3. query: conditions as text
Inside query you can write and, or, not, in, and refer to columns by name. @limit uses the Python variable. Column names with spaces need backticks: `order date` > '2025-06-01'.
- isin([...])
- True if the value is in the list.
- between(a, b)
- True if a ≤ value ≤ b (both ends included by default).
- query("qty > 2 and store_id == 'S01'")
- Conditions as text; use @name for Python variables.
- Physical meaning
- These do exactly what masks do (Lesson 2.3); they just read closer to the question you were asked, which makes mistakes easier to spot.
“between excludes the ends.” By default it includes both: between(2, 4) keeps 2, 3 and 4. Use inclusive=‘left’ or others to change that.
Watch it happen, one step at a time:
df.store_id.isin(['S01', 'S02'])df.qty.between(2, 4)df[df.store_id.isin([...]) & df.qty.between(2, 4)]df.query("store_id in ['S01', 'S02'] and 2 <= qty <= 4"): same rows.4. Your turn
How it works
Principle: all three build the same True/False mask as Lesson 2.3. isin uses a fast lookup set, so it stays quick with thousands of values; query parses the text into the same operations.
When to use which: brackets and & are the most general and work everywhere; query reads best for long filters and in method chains; isin and between replace chains of | and pairs of >= / <=.
5. Practice
- Rewrite
df[(df.c == 'a') | (df.c == 'b') | (df.c == 'd')]withisin. - Write a
queryfor revenue over a variabletarget. - How would you exclude a list of test customer ids?
Answers
df[df['c'].isin(['a', 'b', 'd'])].df.query('revenue > @target').df[~df['customer_id'].isin(test_ids)].
New words: isin, between, query, @variable.
- isin for “one of these values”.
- between for ranges, both ends included by default.
- query writes conditions as readable text; @var uses a Python variable.
- All three produce the same kind of mask as plain conditions.
Check yourself
Sources
- pandas documentation, User Guide: Indexing and selecting data (isin, query) https://pandas.pydata.org/docs/user_guide/indexing.html#the-query-method
- McKinney, W. (2022), Python for Data Analysis, 3rd edition, O'Reilly (open access) https://wesmckinney.com/book/
Your ticks stay in this browser. Save them as a file on the course home.
Updated 2026-11-22. All datasets are made up. Python runs in your browser (the first run downloads it once, about 20 MB); nothing you type is sent anywhere.