Multiple conditions with & and | in Pandas - Time & Space Complexity
We want to understand how the time needed to filter data with multiple conditions changes as the data grows.
How does combining conditions with & and | affect the work done?
Analyze the time complexity of the following code snippet.
import pandas as pd
n = 100 # example size
df = pd.DataFrame({
'A': range(n),
'B': range(n, 0, -1),
'C': ['x', 'y'] * (n // 2)
})
filtered = df[(df['A'] > 10) & ((df['B'] < 50) | (df['C'] == 'x'))]
This code filters rows in a DataFrame using multiple conditions combined with & (and) and | (or).
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each row against the conditions.
- How many times: Once per row, so
ntimes wherenis the number of rows.
Each row is checked once for all conditions combined. As the number of rows grows, the total checks grow proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows directly with the number of rows.
Time Complexity: O(n)
This means the time to filter grows in a straight line with the number of rows.
[X] Wrong: "Using multiple conditions with & and | makes the filtering much slower, like multiplying the time by the number of conditions."
[OK] Correct: The conditions are checked together for each row in one pass, so time grows with rows, not conditions count.
Understanding how filtering with multiple conditions scales helps you explain data processing efficiency clearly and confidently.
What if we added a new condition that requires scanning another column? How would the time complexity change?
Practice
age is greater than 30 and salary is above 50000?Solution
Step 1: Understand the condition type
We want rows where both conditions are true, so this is an AND condition.Step 2: Choose the correct pandas operator
In pandas, & is used for AND between conditions, and each condition must be in parentheses.Final Answer:
Use & to combine the two conditions with parentheses around each. -> Option CQuick Check:
AND condition = & with parentheses [OK]
- Using 'and' instead of & causes errors.
- Not wrapping each condition in parentheses.
- Using | instead of & for AND conditions.
df for rows where df['A'] > 10 or df['B'] < 5?Solution
Step 1: Identify the condition type
The question asks for rows where either condition is true, so this is an OR condition.Step 2: Use correct pandas syntax for OR
In pandas, | is used for OR, and each condition must be wrapped in parentheses inside the DataFrame filter.Final Answer:
df[(df['A'] > 10) | (df['B'] < 5)] -> Option AQuick Check:
OR condition = | with parentheses [OK]
- Using & instead of | for OR conditions.
- Not wrapping conditions in parentheses.
- Using Python 'or' instead of | causes errors.
df below:import pandas as pd
df = pd.DataFrame({'age': [25, 40, 35, 28], 'salary': [40000, 60000, 50000, 45000]})
filtered = df[(df['age'] > 30) & (df['salary'] >= 50000)]
print(filtered)What will be the output?
Solution
Step 1: Check each row against conditions
Row 0: age=25 (no), salary=40000 (no)
Row 1: age=40 (yes), salary=60000 (yes)
Row 2: age=35 (yes), salary=50000 (yes)
Row 3: age=28 (no), salary=45000 (no)Step 2: Select rows where both conditions are true
Rows 1 and 2 satisfy both conditions.Final Answer:
Rows with age > 30 and salary >= 50000: rows 1 and 2 -> Option BQuick Check:
Both conditions true for rows 1 and 2 [OK]
- Confusing & with | and selecting wrong rows.
- Ignoring parentheses causing syntax errors.
- Misreading >= as > or vice versa.
df[(df['score'] > 80) & df['age'] < 30]
Choose the best fix.
Solution
Step 1: Identify the syntax error
The second condition df['age'] < 30 is not wrapped in parentheses, which is required when using &.Step 2: Correct the syntax by adding parentheses
Wrap the second condition in parentheses to fix the error: df[(df['score'] > 80) & (df['age'] < 30)]Final Answer:
Add parentheses around the second condition: df[(df['score'] > 80) & (df['age'] < 30)] -> Option DQuick Check:
Each condition must be in parentheses with & [OK]
- Not using parentheses around all conditions.
- Using 'and' instead of &.
- Changing & to | without reason.
df with columns temperature and humidity. You want to select rows where temperature is between 20 and 30 inclusive, or humidity is below 40. Which of the following code snippets correctly applies these conditions?Solution
Step 1: Understand the condition logic
We want rows where temperature is between 20 and 30 (both conditions combined with AND), OR humidity is below 40.Step 2: Apply correct operator precedence with parentheses
Use parentheses to group the temperature range conditions with & and then combine with | for humidity condition.Final Answer:
df[((df['temperature'] >= 20) & (df['temperature'] <= 30)) | (df['humidity'] < 40)] -> Option AQuick Check:
Group AND conditions, then OR with other condition [OK]
- Missing parentheses causing wrong logic.
- Mixing & and | without grouping.
- Using | inside AND group incorrectly.
Ready to prove it?
Pass the test and this pattern moves unknown
