Creating DataFrame from list of lists in Pandas - Performance & Efficiency
We want to understand how the time needed to create a DataFrame from a list of lists changes as the list grows.
How does the work increase when we add more rows or columns?
Analyze the time complexity of the following code snippet.
import pandas as pd
# List of lists with data
data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# Create DataFrame from list of lists
df = pd.DataFrame(data, columns=['A', 'B', 'C'])
print(df)
This code creates a DataFrame from a list of lists, where each inner list is a row.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading each element in the list of lists to build the DataFrame.
- How many times: Once for each element in the input data (rows × columns).
As the number of rows and columns grows, the work grows by the total number of elements.
| Input Size (rows × columns) | Approx. Operations |
|---|---|
| 10 × 3 = 30 | About 30 operations |
| 100 × 3 = 300 | About 300 operations |
| 1000 × 3 = 3000 | About 3000 operations |
Pattern observation: The time grows roughly in direct proportion to the total number of elements.
Time Complexity: O(n × m)
This means the time to create the DataFrame grows linearly with the number of rows (n) times the number of columns (m).
[X] Wrong: "Creating a DataFrame from a list of lists is always very fast and does not depend on data size."
[OK] Correct: The time actually depends on how many elements are in the list. More rows or columns mean more work to build the DataFrame.
Understanding how data size affects DataFrame creation helps you reason about performance in real projects and interviews.
What if we changed the input from a list of lists to a list of dictionaries? How would the time complexity change?