All lessons

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.

Key points, meanings and formulas on one page: choose "Save as PDF" in the print box.

Module 2 · about 15 minutes · uses Lesson 2.3. You’ll be able to write common filters in fewer, clearer characters.

isin, between and query are readable shortcuts for common filters: one of a list, within a range, and conditions written like a sentence.

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

Pythonorders.csv

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

Pythonorders.csv

3. query: conditions as text

Pythonorders.csv

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:

The question. Lines from S01 or S02 with qty from 2 to 4.
isin. df.store_id.isin(['S01', 'S02'])
between. df.qty.between(2, 4)
Together. df[df.store_id.isin([...]) & df.qty.between(2, 4)]
As query. df.query("store_id in ['S01', 'S02'] and 2 <= qty <= 4"): same rows.

4. Your turn

Your turnproducts.csv
Your turnorders.csv
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

  1. Rewrite df[(df.c == 'a') | (df.c == 'b') | (df.c == 'd')] with isin.
  2. Write a query for revenue over a variable target.
  3. How would you exclude a list of test customer ids?
Answers
  1. df[df['c'].isin(['a', 'b', 'd'])].
  2. df.query('revenue > @target').
  3. 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

1Keep rows where store_id is S01, S02 or S05:
2In query, how do you use a Python variable called limit?
3Does s.between(2, 4) include 2 and 4?

Sources

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.