0
0
Pandasdata~5 mins

Creating DataFrame from list of lists in Pandas - Performance & Efficiency

Choose your learning style9 modes available
Time Complexity: Creating DataFrame from list of lists
O(n × m)
Understanding Time Complexity

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?

Scenario Under Consideration

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 Repeating Operations

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).
How Execution Grows With Input

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 = 30About 30 operations
100 × 3 = 300About 300 operations
1000 × 3 = 3000About 3000 operations

Pattern observation: The time grows roughly in direct proportion to the total number of elements.

Final Time Complexity

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).

Common Mistake

[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.

Interview Connect

Understanding how data size affects DataFrame creation helps you reason about performance in real projects and interviews.

Self-Check

What if we changed the input from a list of lists to a list of dictionaries? How would the time complexity change?