Boolean filtering in Data Analysis Python - Time & Space Complexity
We want to understand how the time to filter data grows as the data size increases.
How does the filtering step scale when we check each item for a condition?
Analyze the time complexity of the following code snippet.
import pandas as pd
data = pd.DataFrame({
'age': [23, 45, 12, 36, 27],
'score': [88, 92, 79, 85, 90]
})
filtered_data = data[data['age'] > 25]
This code filters rows where the 'age' value is greater than 25.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each row's 'age' value against 25.
- How many times: Once for every row in the data.
As the number of rows grows, the number of checks grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 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 as the data gets bigger.
[X] Wrong: "Filtering is instant no matter how big the data is."
[OK] Correct: Each row must be checked, so more data means more work and more time.
Understanding how filtering scales helps you explain data processing speed clearly and confidently.
"What if we filter using two conditions combined with AND? How would the time complexity change?"
Practice
Solution
Step 1: Understand boolean filtering purpose
Boolean filtering is used to pick rows that meet a true condition.Step 2: Compare options to definition
Only Selects rows where a condition is true describes selecting rows based on true conditions.Final Answer:
Selects rows where a condition is true -> Option DQuick Check:
Boolean filtering = Select rows true condition [OK]
- Confusing filtering with sorting
- Thinking it changes data types
- Assuming it removes duplicates
age is greater than 30 in a DataFrame df?Solution
Step 1: Identify correct boolean filtering syntax
The correct way is to put the condition inside square brackets after the DataFrame.Step 2: Check each option
df[df['age'] > 30] uses df[df['age'] > 30], which is correct syntax. df['age' > 30] is invalid syntax. df.age[df.age > 30] incorrectly filters only the age column, returning a Series not DataFrame. df[df['age'] < 30] filters less than 30, not greater.Final Answer:
df[df['age'] > 30] -> Option BQuick Check:
Correct syntax = df[df['age'] > 30] [OK]
- Missing brackets around condition
- Using wrong comparison operator
- Not quoting column name
df below:import pandas as pd
df = pd.DataFrame({
'name': ['Anna', 'Bob', 'Cara', 'Dan'],
'score': [85, 92, 78, 90],
'passed': [True, True, False, True]
})
What will
df[df['score'] > 80 and df['passed']] return?Solution
Step 1: Analyze the boolean expression
The expression uses and without parentheses around each condition, which causes a ValueError in pandas.Step 2: Understand pandas boolean filtering rules
Each condition must be in parentheses when combined with & or |. So df['score'] > 80 and df['passed'] raises ValueError.Final Answer:
ValueError due to missing parentheses -> Option AQuick Check:
Use parentheses around conditions with & or | [OK]
- Not using parentheses around conditions
- Confusing & with and
- Expecting output without fixing syntax
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3, 4]})
filtered = df[df['A'] > 2 or df['A'] < 4]What is the error and how to fix it?
Solution
Step 1: Identify the error cause
The expression df['A'] > 2 or df['A'] < 4 lacks parentheses around each condition, causing a ValueError.Step 2: Fix the error by adding parentheses
Correct syntax is df[(df['A'] > 2) | (df['A'] < 4)] to ensure proper evaluation.Final Answer:
ValueError: Use parentheses around conditions with or -> Option AQuick Check:
Parentheses needed around each condition with | or & [OK]
- Forgetting parentheses around conditions
- Using Python 'or' instead of |
- Assuming no error with missing parentheses
df with columns age and income. You want to filter rows where age is between 25 and 40 inclusive, and income is above 50000. Which code correctly applies boolean filtering?Solution
Step 1: Understand the filtering conditions
We want age between 25 and 40 inclusive, so age >= 25 and age <= 40, and income > 50000.Step 2: Check correct syntax for combining conditions
Conditions must be in parentheses and combined with &. df[(df['age'] >= 25) & (df['age'] <= 40) & (df['income'] > 50000)] correctly uses parentheses and & operators.Step 3: Identify errors in other options
df[df['age'] >= 25 & df['age'] <= 40 & df['income'] > 50000] lacks parentheses around each condition, causing errors. df[(df['age'] > 25) | (df['age'] < 40) & (df['income'] > 50000)] uses | instead of & incorrectly. df[df['age'] >= 25 and df['age'] <= 40 and df['income'] > 50000] uses Python 'and' which does not work with pandas Series.Final Answer:
df[(df['age'] >= 25) & (df['age'] <= 40) & (df['income'] > 50000)] -> Option CQuick Check:
Use parentheses and & for multiple conditions [OK]
- Using Python 'and' instead of &
- Missing parentheses around conditions
- Mixing & and | incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
