All lessons

Module 2: Selecting and filtering · Lesson 2.3 · about 20 min

Filtering rows with conditions

Keep the rows you need with conditions: comparisons, & for and, | for or, ~ for not, brackets around each part, and filters on text and dates.

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

Module 2 · about 20 minutes · uses Lesson 1.3. You’ll be able to filter rows with one or many conditions, on numbers, text and dates.

A condition on a column makes a True/False mask, and the table keeps only the rows where the mask is True.

In everyday terms: a sieve with holes shaped by your rule. Pour the table through and only the rows that fit the rule stay.

1. One condition

A condition makes a True/False Series (a mask); putting it in the brackets keeps the True rows.

Pythonorders.csv

Comparisons: ==, !=, >, >=, <, <=.

2. Combining conditions

& and, | or, ~ not. Wrap each condition in brackets.

Pythonorders.csv

Long filters read better as named masks: is_web = ..., is_big = ..., then orders[is_web & is_big].

3. Text and dates

Pythonorders.csv
df['qty'] > 2
A mask: one True/False per row.
df[mask]
Rows where the mask is True.
&, |, ~
And, or, not; wrap each condition in brackets.
Physical meaning
A mask is a column of ticks and crosses. Combining masks is combining ticks: & needs both ticked, | needs either. The filter never changes the rows it keeps.

“Use and / or between conditions.” Python’s and works on single values, not columns, and raises an error. Use & and |, with brackets: df[(df.qty > 2) & (df.store_id == ‘S01’)].

Watch it happen, one step at a time:

The table.
storeqty
S013
WEB1
S011
S024
Mask 1. qty > 2 → True, False, False, True.
Mask 2. store == 'S01' → True, False, True, False.
Both (&). True, False, False, False.
Filter. Only the first row stays:
storeqty
S013

4. Your turn

Your turnorders.csv
Your turnorders.csv
How it works

Principle: a filter is two steps: build a mask (one True/False per row), then index with it. Because masks are Series, you can store, combine, count (mask.sum()) and average them (mask.mean() is the share of True).

Why the brackets: in Python & is evaluated before >=, so df.qty >= 2 & df.store == 'S01' is read as df.qty >= (2 & df.store) == 'S01' and fails. Brackets make the order explicit.

Why not and: and asks “is this whole Series true?”, which has no single answer, so pandas raises “The truth value of a Series is ambiguous”.

5. Practice

  1. Write a filter for qty between 2 and 4, inclusive.
  2. What share of lines have a discount? (One expression.)
  3. What does the error “The truth value of a Series is ambiguous” usually mean?
Answers
  1. df[(df['qty'] >= 2) & (df['qty'] <= 4)] or df[df['qty'].between(2, 4)].
  2. (df['discount'] > 0).mean().
  3. You used and/or (or if) with a Series; use & / | with brackets.

New words: mask, boolean indexing, and (&), or (|), not (~).

  • Conditions make boolean masks; df[mask] keeps True rows.
  • Combine with & (and), | (or), ~ (not), each condition in brackets.
  • mask.sum() counts matching rows; mask.mean() gives the share.
  • Filtering never changes values, only which rows are kept.

Check yourself

1Which keeps rows where qty is at least 2 AND store_id is 'S01'?
2What does ~ do in df[~mask]?
3How do you keep the orders from 2026 when date is a datetime column?

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.