Skip to content
Justice Tech School

Combine Conditions

Combine conditions with & (and), | (or) and ~ (not). Wrap each condition in parentheses.

Example
import pandas as pd
import numpy as np
df = pd.DataFrame({'name': ['Ada', 'Bob', 'Cy', 'Di'], 'age': [30, 25, 35, 28], 'city': ['Lagos', 'Accra', 'Lagos', 'Nairobi'], 'score': [88.5, 92.0, 79.5, 85.0]})
a = df[(df['age'] > 26) & (df['city'] == 'Lagos')]
b = df[(df['score'] > 90) | (df['age'] < 26)]
c = df[~(df['city'] == 'Lagos')]
print(a['name'].tolist(), b['name'].tolist(), c['name'].tolist())

Result

['Ada', 'Cy'] ['Bob'] ['Bob', 'Di']

Exercise

And operator.

(a > 1)  (b > 1)

Quiz Test

Why parentheses?

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