Skip to main content

Create a DataFrame from List - Solution & Explanation

Easy6 min readAsked at: Amazon, Microsoft, Meta +2
Practice this problem

Problem Statement

Write a solution to create a DataFrame from a 2D list called student_data. This 2D list contains the IDs and ages of some students.

The DataFrame should have two columns, student_id and age, and be in the same order as the original 2D list.

The result format is in the following example.

 

Example 1:

Input:
student_data:
[
  [1, 15],
  [2, 11],
  [3, 11],
  [4, 20]
]
Output:
+------------+-----+
| student_id | age |
+------------+-----+
| 1          | 15  |
| 2          | 11  |
| 3          | 11  |
| 4          | 20  |
+------------+-----+
Explanation:
A DataFrame was created on top of student_data, with two columns named student_id and age.

Approach Overview

Problem Overview: You are given a list of records and a set of column names. The task is to construct a tabular structure (a DataFrame) where each inner list represents a row and each value maps to its corresponding column. The goal is simply to transform the raw list structure into a structured table format.

Approach 1: Using pandas DataFrame Constructor (O(n) time, O(n) space)

In Python, the most direct solution uses the pandas library. The pd.DataFrame() constructor accepts a list of rows and a list of column names, then internally builds the tabular structure. Each inner list becomes a row and values are aligned with the provided column labels. The constructor performs a linear pass over the data, giving O(n) time complexity where n is the total number of elements across rows. This approach is preferred when working in data science or analytics environments where pandas is already part of the workflow.

Approach 2: Manual Construction using Native Arrays/Lists (O(n) time, O(n) space)

Languages without a built-in DataFrame abstraction (C, C++, Java, C#, JavaScript) require constructing the structure manually. Iterate through the input list and store each row in a 2D array or list-of-lists structure. Column names can be stored separately in an array or mapped to indices using a dictionary if column lookup is required. The algorithm performs a simple iteration over the dataset and copies values into the resulting structure. Because every element is processed once, the time complexity is O(n) and the additional memory required is also O(n).

This approach mirrors how tabular data is internally represented in many systems: rows stored sequentially with columns mapped by index. It is common when implementing lightweight table structures or when solving problems involving arrays and basic data processing.

Recommended for interviews: Interviewers usually expect the straightforward construction logic rather than heavy library usage. Showing the manual list/array construction demonstrates you understand how tabular data structures work internally. The pandas solution is still useful in Python-centric environments because it leverages a standard library designed for data manipulation.

Approach 1: Using pandas library in Python

This approach utilizes the Python pandas library, which is specifically designed for data manipulation and analysis. We will simply pass the list to the DataFrame constructor along with column labels.

This solution uses the pandas library to create a DataFrame. We call the DataFrame constructor with the 2D list and specify the column names. The returned DataFrame has the desired structure.

Code

Python

Complexity

Time Complexity: O(n) where n is the number of rows in the student_data list.
Space Complexity: O(n) as the data is stored in a DataFrame.

Try this approach in the editor →

Approach 2: Manual Construction using Native Lists/Arrays

This method manually constructs a DataFrame-like structure using native programming constructs like lists, arrays, dictionaries, etc. This involves manually setting up structures to represent columns and rows and maintaining the desired format.

In this C implementation, we define a function to print the given 2D array in a tabular format. We use standard I/O functions to format each entry like a DataFrame using loops and print statements.

Code

C

C++

Java

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the number of rows in the student_data array.
Space Complexity: O(1) since we're modifying and printing directly without additional data structures.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Using pandas library in Python

Time Complexity: O(n) where n is the number of rows in the student_data list.
Space Complexity: O(n) as the data is stored in a DataFrame.

Manual Construction using Native Lists/Arrays

Time Complexity: O(n), where n is the number of rows in the student_data array.
Space Complexity: O(1) since we're modifying and printing directly without additional data structures.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
pandas DataFrame Constructor (Python)O(n)O(n)When working in Python data analysis environments where pandas is available
Manual Construction with Arrays/ListsO(n)O(n)General approach for languages without DataFrame support such as C, C++, Java, C#, or JavaScript

Video Solution

2877. Create a DataFrame from List | LeetCode | Python | Pandas • You Data And AI • 2,747 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Create a DataFrame from List easy or hard?
Create a DataFrame from List is classified as an easy problem. The task mainly tests understanding of tabular data representation and basic list or array manipulation, with an O(n) construction algorithm.
Create a DataFrame from List Python/Java solution
In Python, the typical solution is pd.DataFrame(data, columns=columns) from the pandas library, which converts the list directly into a DataFrame. In Java or similar languages, create a List<List<T>> or 2D array and store rows manually while keeping column names in a separate array or map.
How to solve Create a DataFrame from List in O(n)?
Iterate through the input list once and map each row into a structured table. In Python, use pandas with pd.DataFrame(data, columns=column_names), which internally processes the dataset in linear time. In other languages, store rows in a 2D array or list structure while assigning column positions by index.
What is the best approach for Create a DataFrame from List?
The best approach depends on the language. In Python, the pandas DataFrame constructor is the most efficient and concise because it directly converts a list of rows into a structured table in O(n) time. In languages without pandas, the standard solution is to manually build a 2D array or list-of-lists while mapping column names to indices.
Is Create a DataFrame from List asked at Google/Amazon/Meta?
This problem reflects common data transformation tasks seen in data engineering and analytics interviews. Variations appear in companies that evaluate Python or data manipulation skills, particularly roles involving pandas, ETL pipelines, or structured data processing.
What data structure is used in Create a DataFrame from List?
The underlying structure is essentially a 2D array or list-of-lists where each inner list represents a row. Column names are stored separately and mapped to indices. Libraries like pandas wrap this structure with additional indexing and vectorized operations.
What is the time complexity of Create a DataFrame from List?
The time complexity is O(n), where n is the total number of elements across all rows in the input list. Each value must be processed once when building the DataFrame or copying elements into a 2D array structure. Space complexity is also O(n) because the resulting table stores all elements.

Ready to solve this problem?

Practice Create a DataFrame from List with our built-in code editor and test cases.

Practice on FleetCode