pivot_table() for summarization in Pandas - Time & Space Complexity
We want to understand how the time needed to summarize data with pivot_table() changes as the data grows.
How does the work increase when we have more rows or categories?
Analyze the time complexity of the following code snippet.
import pandas as pd
df = pd.DataFrame({
'Category': ['A', 'B', 'A', 'B', 'C'],
'Values': [10, 20, 30, 40, 50]
})
summary = df.pivot_table(index='Category', values='Values', aggfunc='sum')
This code groups data by 'Category' and sums the 'Values' for each group.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning all rows to group by categories.
- How many times: Once for each row in the data.
As the number of rows grows, the time to scan and group grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 scans and group updates |
| 100 | About 100 scans and group updates |
| 1000 | About 1000 scans and group updates |
Pattern observation: The work grows directly with the number of rows.
Time Complexity: O(n)
This means the time needed grows in a straight line as the data size grows.
[X] Wrong: "pivot_table() runs in constant time no matter the data size."
[OK] Correct: The function must look at every row to group and summarize, so more data means more work.
Knowing how grouping and summarizing scales helps you explain data processing choices clearly and confidently.
"What if we added multiple columns to group by in pivot_table()? How would the time complexity change?"