Boolean indexing in Pandas - Time & Space Complexity
We want to understand how the time needed to filter data using Boolean indexing changes as the data grows.
How does the filtering time grow when we have more rows in our data?
Analyze the time complexity of the following code snippet.
import pandas as pd
df = pd.DataFrame({
'age': [23, 45, 12, 36, 27],
'score': [88, 92, 79, 95, 85]
})
filtered = df[df['age'] > 25]
This code creates a table with ages and scores, then selects rows where age 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 table.
As the number of rows grows, the time to check each row grows too, roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: Doubling the rows doubles the number of checks needed.
Time Complexity: O(n)
This means the time to filter grows linearly with the number of rows in the data.
[X] Wrong: "Filtering with Boolean indexing is instant no matter how big the data is."
[OK] Correct: Each row must be checked, so more rows mean more work and more time.
Understanding how filtering scales helps you write efficient data code and explain your choices clearly.
"What if we filter using multiple conditions combined with && or ||? How would the time complexity change?"
Practice
boolean indexing in pandas do?Solution
Step 1: Understand boolean indexing
Boolean indexing uses True/False values to filter rows in a DataFrame.Step 2: Identify its purpose
It selects only rows where the condition is True, filtering data easily.Final Answer:
Selects rows based on True/False conditions -> Option CQuick Check:
Boolean indexing = row filtering [OK]
- Confusing boolean indexing with sorting
- Thinking it changes data types
- Mixing it up with DataFrame merging
'age' is greater than 30 in a DataFrame df?Solution
Step 1: Check correct boolean indexing syntax
Use df[condition], where condition is df['age'] > 30.Step 2: Identify syntax errors in other options
df['age' > 30] misplaces brackets, C uses assignment (=) instead of comparison, D uses invalid operator =>.Final Answer:
df[df['age'] > 30] -> Option BQuick Check:
Correct syntax uses df[condition] with comparison [OK]
- Using = instead of > for comparison
- Wrong bracket placement
- Using invalid operators like =>
df below:name age score 0 Anna 25 88 1 Bob 32 75 2 Cara 29 95 3 Dave 35 60 4 Ella 28 82
What will
df[(df['age'] > 28) & (df['score'] > 70)] return?Solution
Step 1: Identify rows where age > 28
Rows: Bob (32), Cara (29), Dave (35) satisfy age > 28.Step 2: From those, select rows with score > 70
Bob (75), Cara (95), Dave (60). Dave fails score > 70, so only Bob and Cara remain.Final Answer:
Rows with age > 28 and score > 70: Bob and Cara -> Option DQuick Check:
Filter by both conditions = Bob and Cara [OK]
- Including Dave who has score 60
- Mixing up & and | operators
- Ignoring parentheses around conditions
score is above 80?df[df['score'] > 80 & df['age'] < 30]
Solution
Step 1: Understand operator precedence in pandas boolean indexing
Bitwise operators (&, |) require parentheses around each condition to avoid errors.Step 2: Identify missing parentheses
Code lacks parentheses arounddf['score'] > 80anddf['age'] < 30, causing wrong evaluation.Final Answer:
Missing parentheses around conditions causing operator precedence error -> Option AQuick Check:
Use parentheses with & and | [OK]
- Forgetting parentheses around each condition
- Confusing & with logical and (and)
- Assuming no error without parentheses
df with columns 'city', 'temperature', and 'humidity'. You want to select rows where city is either 'Paris' or 'London' and temperature is above 20. Which code correctly applies boolean indexing?Solution
Step 1: Understand condition grouping with parentheses
Conditions combined with | must be wrapped in parentheses to group city checks correctly.Step 2: Check each option for correct syntax and logic
df[((df['city'] == 'Paris') | (df['city'] == 'London')) & (df['temperature'] > 20)] correctly groups city conditions and combines with temperature condition using &.Final Answer:
df[((df['city'] == 'Paris') | (df['city'] == 'London')) & (df['temperature'] > 20)] -> Option AQuick Check:
Group OR conditions with parentheses before AND [OK]
- Missing parentheses causing wrong condition order
- Using Python 'or' instead of |
- Trying to compare city to ('Paris' or 'London') directly
Ready to prove it?
Pass the test and this pattern moves unknown
