Watch 10 video solutions for Create a DataFrame from List, a easy level problem. This walkthrough by You Data And AI has 2,715 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 namedstudent_idandage.
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 | Time | Space | When 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/Lists | O(n) | O(n) | General approach for languages without DataFrame support such as C, C++, Java, C#, or JavaScript |