Module 06 · Core

Filter with conditions

Build Boolean masks and combine conditions to answer precise questions.

Before you start: Know column selection and the basic comparison operators.

Python loads from this site on your first run.

Each run starts with fresh data. Download any exported files before another run. Your written answers and code drafts are saved on this device; export your work to keep a backup.

M06C01 · Explore · predict · practice

See the True and False values first

Comparing a Series with a value produces a Boolean Series, often called a mask. Each True marks a row that meets the condition. df.loc[mask, columns] keeps those rows and selects the requested columns. Keeping the mask in its own variable makes the logic easier to inspect.

Read the example

df = students()
mask = df["Score"] > 80
print(mask)
result = df.loc[mask, ["Student_ID", "Name", "Score"]]
print(result)
Verified example output · pandas 2.2.3
0      True
1     False
2      True
3     False
4     False
5      True
6      True
7     False
8     False
9      True
10    False
11     True
Name: Score, dtype: bool
   Student_ID    Name  Score
0        S001   Amira     88
2        S003   Chloe     91
5        S006  Fatima     95
6        S007   Grace     84
9        S010   Jamal     89
11       S012    Lina     82

Watch for this. A mask should correspond to the table being filtered; a mask from another table can misalign.

Your turn

Change the condition to Score < 70. Select Student_ID, Name, and Score into result. Print the number of matches.

For the code check, keep these result variable names: mask, result. Different valid approaches are welcome.

Show a hint

Change > to < and use 70 as the threshold.

Show one worked solution
df = students()
mask = df["Score"] < 70
result = df.loc[mask, ["Student_ID", "Name", "Score"]]
print(result)
print("Matches:", len(result))

Dana, Hassan, and Khalid match the new rule, giving three rows. The question changed from high scores to scores needing attention.

Compare your explanations
  1. Read. A Boolean value, True or False, for this clean non-missing Score column.
  2. Predict. Six: S001, S003, S006, S007, S010, and S012.
  3. Explain. It only contains the condition results. A second selection uses that mask to retrieve the matching records.

One quick check

What does a True in a filter mask mean?

What you should be able to explain. I can separate the condition from the table selection it controls.

M06C02 · Explore · predict · practice

Combine conditions with AND and OR

Use & for element-by-element AND and | for element-by-element OR. Put parentheses around each comparison before combining them. AND requires both conditions; OR requires at least one. Python's and and or do not combine pandas Series masks this way.

Read the example

df = students()
mask = (df["Campus"] == "RAK") & (df["Score"] >= 80)
result = df.loc[mask, ["Student_ID", "Name", "Campus", "Score"]]
print(result)
Verified example output · pandas 2.2.3
  Student_ID   Name Campus  Score
0       S001  Amira    RAK     88
2       S003  Chloe    RAK     91
6       S007  Grace    RAK     84

Watch for this. Do not omit the parentheses around comparisons; operator precedence can change the expression.

Your turn

Select students who are at RAK OR have Score at least 90. Keep the same four output columns.

For the code check, keep these result variable names: result. Different valid approaches are welcome.

Show a hint

Use | between two parenthesized comparisons.

Show one worked solution
df = students()
mask = (df["Campus"] == "RAK") | (df["Score"] >= 90)
result = df.loc[mask, ["Student_ID", "Name", "Campus", "Score"]]
print(result)

All six RAK students qualify, and Fatima qualifies through her score of 95. Chloe meets both conditions but appears only once, so there are seven rows.

Compare your explanations
  1. Read. Its Campus must equal RAK and its Score must be at least 80.
  2. Predict. Amira, Chloe, and Grace.
  3. Explain. A student can pass either condition: be at RAK, have Score at least 80, or satisfy both. More rows can qualify.

One quick check

Which correctly combines two pandas conditions with AND?

What you should be able to explain. I can translate both, either, and at least into correct filter logic.

M06C03 · Explore · predict · practice

Membership, ranges, and NOT

isin() tests whether a value belongs to an allowed list. between() checks a range; inclusive="both" includes its two endpoints. The ~ operator reverses a Boolean mask. Naming masks can make a longer rule easier to explain.

Read the example

df = students()
in_program = df["Program"].isin(["AI"])
in_range = df["Score"].between(70, 90, inclusive="both")
result = df.loc[in_program & in_range, ["Name", "Program", "Score"]]
print(result)
Verified example output · pandas 2.2.3
    Name Program  Score
0  Amira      AI     88
6  Grace      AI     84
9  Jamal      AI     89

Watch for this. Do not use Program == ["AI", "IT"] to test membership; use .isin(["AI", "IT"]).

Your turn

Select students whose Program is NOT AI and whose Score is between 70 and 85 inclusive. Print their names, programs, and scores.

For the code check, keep these result variable names: result. Different valid approaches are welcome.

Show a hint

Reverse the membership mask with ~ and combine it with the new range mask.

Show one worked solution
df = students()
not_ai = ~df["Program"].isin(["AI"])
in_range = df["Score"].between(70, 85, inclusive="both")
result = df.loc[not_ai & in_range, ["Name", "Program", "Score"]]
print(result)

Bilal, Ethan, Ivy, and Lina qualify. ~ reverses the AI membership test before the score range is applied.

Compare your explanations
  1. Read. Include values exactly equal to 70 or 90, as well as values between them.
  2. Predict. Amira, Grace, and Jamal.
  3. Explain. isin expects a list-like collection of allowed values. The same expression can later include several categories.

One quick check

Which operator reverses a Boolean Series?

What you should be able to explain. I can express allowed categories, numerical ranges, and excluded categories.

M06C04 · Explore · predict · practice

Read a filter written with query

query() is another way to express a row filter, using a short expression in a string. Column names appear inside that expression. @limit refers to a Python variable outside it. Learn the normal Boolean-mask form first so you can check what the query means.

Read the example

df = students()
limit = 85
result = df.query("Score >= @limit and Campus == 'Dubai'")[["Name", "Campus", "Score"]]
print(result)
Verified example output · pandas 2.2.3
     Name Campus  Score
5  Fatima  Dubai     95
9   Jamal  Dubai     89

Watch for this. Use fixed, trusted query expressions for these lessons; do not construct executable expressions from arbitrary user text.

Your turn

Use a normal Boolean mask instead of query to find Dubai students with Score at least 80. Save the same three columns in result.

For the code check, keep these result variable names: result. Different valid approaches are welcome.

Show a hint

Combine (df["Score"] >= limit) and (df["Campus"] == "Dubai") with &.

Show one worked solution
df = students()
limit = 80
mask = (df["Score"] >= limit) & (df["Campus"] == "Dubai")
result = df.loc[mask, ["Name", "Campus", "Score"]]
print(result)

Fatima, Jamal, and Lina meet the threshold of 80. A mask and a query can express the same selection rule.

Compare your explanations
  1. Read. It tells query to use the Python variable limit instead of looking for a column named limit.
  2. Predict. Fatima and Jamal.
  3. Explain. query parses its expression string. Outside query, combining pandas Series directly requires the element-wise operators such as &.

One quick check

Inside query, what does @limit refer to?

What you should be able to explain. I can translate a simple query into a Boolean mask and explain both.

Further detail: official documentation for this topic.