Exporting to CSV in Data Analysis Python - Time & Space Complexity
When we export data to a CSV file, we want to know how long it takes as the data grows.
We ask: How does the time to save data change when we have more rows?
Analyze the time complexity of the following code snippet.
import pandas as pd
n = 1000 # Example value for n
data = pd.DataFrame({
'A': range(n),
'B': range(n)
})
data.to_csv('output.csv', index=False)
This code creates a table with n rows and exports it to a CSV file.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Writing each row of data to the CSV file.
- How many times: Once for each of the n rows in the data.
As the number of rows grows, the time to write grows roughly the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 write steps |
| 100 | About 100 write steps |
| 1000 | About 1000 write steps |
Pattern observation: The time grows in a straight line as rows increase.
Time Complexity: O(n)
This means the time to export grows directly with the number of rows.
[X] Wrong: "Exporting a CSV is instant no matter how big the data is."
[OK] Correct: Writing each row takes time, so bigger data means longer export time.
Understanding how data export time grows helps you explain performance in real projects.
"What if we export only a few columns instead of all columns? How would the time complexity change?"