Skip to content

2877. Create a DataFrame from List

Instructions

  • 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

Input:

student_data:

plaintext
[
  [1, 15],
  [2, 11],
  [3, 11],
  [4, 20]
]

Output:

student_idage
115
211
311
420

Explanation:

A DataFrame was created on top of student_data, with two columns named student_id and age.

Submissions

python
import pandas as pd

def createDataframe(student_data: List[List[int]]) -> pd.DataFrame:
    df = pd.DataFrame(student_data, columns=['student_id', 'age'])
    return df

Explanation

Python (Pandas)
Submitted by @noeyislearning
  • import pandas as pd: Import the pandas library to work with DataFrames.
  • def createDataframe(student_data: List[List[int]]) -> pd.DataFrame:: Define a function called createDataframe that takes a 2D list student_data as input and returns a DataFrame.
  • df = pd.DataFrame(student_data, columns=['student_id', 'age']): Create a DataFrame df from the 2D list student_data with columns named student_id and age.
  • return df: Return the DataFrame df.