Selecting rows by condition in Pandas - Time & Space Complexity
We want to know how the time to select rows by a condition changes as the data grows.
How does the work increase when the table gets bigger?
Analyze the time complexity of the following code snippet.
import pandas as pd
df = pd.DataFrame({'age': [23, 45, 12, 36, 27] * 200})
selected = df[df['age'] > 30]
This code creates a table with ages and selects rows where age is greater than 30.
- Primary operation: Checking each row's 'age' value against 30.
- How many times: Once for every row in the table.
As the number of rows grows, the time to check each row grows at the same rate.
| 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 select rows grows in a straight line as the table gets bigger.
[X] Wrong: "Selecting rows by condition is instant no matter the size."
[OK] Correct: The code must check each row to see if it meets the condition, so more rows mean more work.
Understanding how filtering data scales helps you write efficient code and explain your choices clearly.
"What if we select rows using multiple conditions combined with AND? How would the time complexity change?"