Standardizing column names in Pandas - Time & Space Complexity
We want to understand how the time needed to standardize column names changes as the number of columns grows.
How does the work increase when we have more columns to rename?
Analyze the time complexity of the following code snippet.
df.columns = [col.strip().lower().replace(' ', '_') for col in df.columns]
This code changes all column names to lowercase, removes spaces, and replaces them with underscores.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over each column name to apply string changes.
- How many times: Once for each column in the DataFrame.
As the number of columns increases, the time to process all names grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 string operations |
| 100 | 100 string operations |
| 1000 | 1000 string operations |
Pattern observation: Doubling the number of columns roughly doubles the work.
Time Complexity: O(n)
This means the time needed grows directly with the number of columns.
[X] Wrong: "Changing column names is instant no matter how many columns there are."
[OK] Correct: Each column name must be processed one by one, so more columns mean more work and more time.
Understanding how simple operations scale helps you explain your code choices clearly and shows you think about efficiency in real tasks.
"What if we used a function that also checked each column name's length before changing it? How would the time complexity change?"