Arithmetic operations on columns in Pandas - Time & Space Complexity
We want to understand how the time to do arithmetic on columns changes as the data grows.
How does the work increase when we have more rows in the table?
Analyze the time complexity of the following code snippet.
import pandas as pd
n = 10 # Example value for n
df = pd.DataFrame({
'A': range(n),
'B': range(n, 2*n)
})
df['C'] = df['A'] + df['B']
This code creates two columns with numbers and adds them to make a new column.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding each pair of numbers from columns A and B.
- How many times: Once for each row in the DataFrame.
As the number of rows grows, the number of additions grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of rows.
Time Complexity: O(n)
This means the time to add columns grows in a straight line as the data gets bigger.
[X] Wrong: "Adding two columns is instant no matter how big the data is."
[OK] Correct: Each row needs one addition, so more rows mean more work and more time.
Knowing how operations grow with data size helps you explain your code choices clearly and confidently.
"What if we added two columns but only for rows where a condition is true? How would the time complexity change?"