2877. Create a DataFrame from List

Problem Description

In this problem, we are tasked with constructing a DataFrame using the pandas library in Python. pandas is a powerful data manipulation library that provides numerous tools to work with structured data. A DataFrame is one of the core structures in pandas, designed to mimic the functionality of a database table or an Excel spreadsheet – essentially, it's a labeled two-dimensional array where each column can be of a different data type.

We are given a 2-dimensional list student_data where each sub-list contains two elements: the first element represents a student's ID, and the second is the student's age. The challenge is to convert this 2D list into a DataFrame with two columns. Those columns are to be named student_id and age.

The DataFrame must maintain the same row order as it was in the provided 2D list. For example, if the input is [[1, 20], [2, 22]], the resulting DataFrame should look like this:


Creating DataFrames from various types of data is a common operation in data analysis, as it facilitates easy data manipulation, filtering, and analysis.


The intuitive approach for solving this problem is to leverage the DataFrame constructor method provided by pandas. This method can accept various forms of input data including lists, dictionaries, and other DataFrames. When given a 2D list, each sub-list is interpreted as a row in the DataFrame.

In order to ensure that the columns of the DataFrame are correctly labeled, we specify the columns parameter when calling the DataFrame constructor, passing in a list with the desired column names: ['student_id', 'age'].

Since the task does not involve any complex transformations, the provided code simply passes the student_data list and the column names to the DataFrame constructor and returns the newly created DataFrame. The rationale is that by directly utilizing the built-in functionality of pandas, we can create the required DataFrame object in an efficient and readable way.

Solution Approach

The solution approach is straightforward, utilizing the pandas library's data structure and functionality without employing complex algorithms or design patterns.

Here are the steps for the implementation:

  1. Import the pandas library to gain access to the DataFrame construction capability.

    1import pandas as pd
  2. Define the function createDataframe which takes one argument, student_data. This argument is expected to be a 2-dimensional list where the inner lists consist of exactly two integers, the student's ID and the student's age.

  3. Inside the function, the DataFrame constructor of pandas is called with two parameters. The first parameter is the student_data list itself, and the second is the columns parameter, which is a list containing the column names, namely ['student_id', 'age'].

    1return pd.DataFrame(student_data, columns=['student_id', 'age'])
  4. The DataFrame constructor interprets each sub-list in student_data as a row in the DataFrame, with the first integer in the sub-list being placed under the student_id column, and the second under the age column.

By adhering to the Pythonic principle of "simple is better than complex," this solution avoids overengineering, relying on the efficient and well-tested internal mechanisms of the pandas library to achieve the desired result.

No special algorithm or additional data structures are required. The pattern used is one of directly mapping the input data to the DataFrame structure. This makes the code highly readable, easy to maintain, and efficient for the problem at hand.

In summary, the approach takes advantage of pandas's built-in functions to transform a 2D list into a structured DataFrame with minimal code, achieving the goal with elegance and efficiency.

Ready to land your dream job?

Unlock your dream job with a 2-minute evaluator for a personalized learning plan!

Start Evaluator

Example Walkthrough

Let's walk through a small example to illustrate the solution approach. Suppose we have the following 2D list which represents student_data:

1student_data = [[101, 18], [102, 19], [103, 20]]

Each sub-list contains a student's ID and the student's age. We want to create a DataFrame from this list where each student's information is a row, with student_id and age as column headers.

By following the steps outlined in the solution approach:

  1. First, we import the pandas library, which is essential for creating DataFrames.

    1import pandas as pd
  2. We define a function createDataframe that will accept student_data as an argument.

    1def createDataframe(student_data):
  3. Within the function, we call the DataFrame constructor of pandas, passing the student_data list and providing the column names ['student_id', 'age'] through the columns parameter.

    1return pd.DataFrame(student_data, columns=['student_id', 'age'])
  4. As a result, the DataFrame constructor interprets each inner list from student_data as a row. For our example, the constructor will create a DataFrame that looks like this:


This process creates a pandas DataFrame with the correct row order and the specified column names. Finally, calling our createDataframe function with student_data:

1df = createDataframe(student_data)

Will output:

1   student_id  age
20         101   18
31         102   19
42         103   20

Our example demonstrates the simplicity and elegance of the solution approach, relying on the powerful pandas library to efficiently create a DataFrame from a 2D list with the desired structure and labels.

Solution Implementation

1import pandas as pd
2from typing import List
4# Function to create a DataFrame from student data
5# Parameters:
6# student_data (List[List[int]]): A list of lists, where each inner list contains student_id, and age.
7# Returns:
8# pd.DataFrame: A DataFrame with columns 'student_id' and 'age' created from the input data.
9def create_dataframe(student_data: List[List[int]]) -> pd.DataFrame:
10    # Create DataFrame using the provided student_data
11    # Assign column names 'student_id' and 'age' to the DataFrame
12    dataframe = pd.DataFrame(student_data, columns=['student_id', 'age'])
13    # Return the created DataFrame
14    return dataframe
1import java.util.List;
2import java.util.ArrayList;
3import javax.swing.table.DefaultTableModel;
5public class DataFrameCreator {
7    /**
8     * Function to create a DefaultTableModel from student data.
9     * It models the concept of a DataFrame in a way that's familiar to Java users.
10     *
11     * Parameters:
12     * studentData (List<List<Integer>>): A list of lists, where each inner list contains student_id, and age.
13     * Returns:
14     * DefaultTableModel: A DefaultTableModel with columns 'student_id' and 'age' created from the input data.
15     */
16    public static DefaultTableModel createDataFrame(List<List<Integer>> studentData) {
17        // Define the column names for the table model
18        String[] columnNames = {"student_id", "age"};
20        // Convert the List of Lists into an array of arrays, as DefaultTableModel requires it.
21        // The outer array corresponds to the rows and the inner one to the columns.
22        Object[][] dataArray = studentData.stream()
23            .map(list -> list.toArray(new Object[0]))
24            .toArray(Object[][]::new);
26        // Create the DefaultTableModel with the data array and the column names
27        DefaultTableModel tableModel = new DefaultTableModel(dataArray, columnNames);
29        // Return the created table model
30        return tableModel;
31    }
33    // Example usage
34    public static void main(String[] args) {
35        // List of lists representing student data (student_id, age)
36        List<List<Integer>> studentData = new ArrayList<>();
37        studentData.add(new ArrayList<Integer>() {{
38            add(1); // student_id
39            add(20); // age
40        }});
41        studentData.add(new ArrayList<Integer>() {{
42            add(2); // student_id
43            add(22); // age
44        }});
46        // Create a DataFrame (DefaultTableModel) from the student data
47        DefaultTableModel dataFrame = createDataFrame(studentData);
49        // Example of printing the data
50        for (int row = 0; row < dataFrame.getRowCount(); row++) {
51            for (int col = 0; col < dataFrame.getColumnCount(); col++) {
52                System.out.print(dataFrame.getValueAt(row, col) + " ");
53            }
54            System.out.println();
55        }
56    }
1#include <iostream>
2#include <vector>
3#include <string>
5// Function to print a simulated DataFrame from student data
6// Parameters:
7// studentData (const std::vector<std::vector<int>>&): A vector of vectors,
8// where each inner vector contains student_id, and age.
9void createDataFrame(const std::vector<std::vector<int>>& studentData) {
10    // Check if the studentData is not empty and each inner vector has a size of 2
11    if (!studentData.empty() && studentData[0].size() == 2) {
12        // Print column names
13        std::cout << "student_id" << '\t' << "age" << std::endl;
15        // Iterate over the studentData to print the values
16        for (const auto& student : studentData) {
17            // Print student_id and age from the inner vector
18            std::cout << student[0] << '\t' << student[1] << std::endl;
19        }
20    } else {
21        // Print an error message if studentData is empty or inner vectors do not have a size of 2
22        std::cerr << "Error: studentData must be a non-empty vector of vectors with a size of 2." << std::endl;
23    }
26int main() {
27    // Example student data: each inner vector contains student_id and age
28    std::vector<std::vector<int>> studentData = {
29        {1, 20},  // Student 1 is 20 years old
30        {2, 22},  // Student 2 is 22 years old
31        {3, 19}   // Student 3 is 19 years old
32    };
34    // Create a simulated DataFrame and print the student data
35    createDataFrame(studentData);
37    return 0;
1// Required type definitions for clarity
2type StudentData = {
3  studentId: number;
4  age: number;
7// Function to create an array of student objects from student data
8// studentData parameter: An array of arrays, where each inner array contains studentId, and age.
9// Returns an array of objects that represent students with properties 'studentId' and 'age'.
10function createStudentArray(studentData: Array<[number, number]>): StudentData[] {
11  // Map each pair of student data to an object with 'studentId' and 'age' properties
12  const students: StudentData[] = studentData.map(([studentId, age]) => ({
13    studentId,
14    age,
15  }));
17  // Return the array of student objects
18  return students;

Time and Space Complexity

The time complexity of creating a dataframe using pandas.DataFrame is generally O(n) where n is the total number of elements in the input list student_data. Each element (a sub-list in this case) is inserted into the DataFrame during creation.

The space complexity is also O(n) since the DataFrame stores all elements of the input list. Each sub-list corresponds to a row in the DataFrame, and each element within a sub-list corresponds to a cell in the DataFrame.

Discover Your Strengths and Weaknesses: Take Our 2-Minute Quiz to Tailor Your Study Plan:
Question 1 out of 10

A heap is a ...?

Recommended Readings

Got a question? Ask the Monster Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.

Coding Interview Strategies

Dive into our free, detailed pattern charts and company guides to understand what each company focuses on.

See Patterns