Skip to content
Justice Tech School

Filtering with Conditions

Combine conditions with & and | and wrap each in parentheses.

Example
df = pd.DataFrame({"name": ["Ada", "Kofi", "Amina", "Tunde"], "country": ["NG", "GH", "NG", "NG"], "score": [92, 78, 85, 71]})
r = df[(df["country"] == "NG") & (df["score"] > 80)]
print(r["name"].tolist())
print(df[(df["score"] < 75) | (df["country"] == "GH")]["name"].tolist())

Result

['Ada', 'Amina']
['Kofi', 'Tunde']

Exercise

And operator.

(a)  (b)

Quiz Test

Why parentheses?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.