Create a DataFrame from List - Problem

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.

Note: The function should return a pandas DataFrame object with the specified column names and data structure.

Input & Output

Example 1 — Basic Student Data
$ Input: student_data = [[1, 15], [2, 11], [3, 11], [4, 20]]
Output: {"student_id": [1, 2, 3, 4], "age": [15, 11, 11, 20]}
💡 Note: Create DataFrame with student_id and age columns from the 2D list. Each inner list becomes a row with ID in first column and age in second column.
Example 2 — Minimum Data
$ Input: student_data = [[5, 18]]
Output: {"student_id": [5], "age": [18]}
💡 Note: Single student data creates DataFrame with one row containing student ID 5 and age 18.
Example 3 — Multiple Students Same Age
$ Input: student_data = [[10, 22], [11, 22], [12, 19]]
Output: {"student_id": [10, 11, 12], "age": [22, 22, 19]}
💡 Note: DataFrame maintains order and allows duplicate ages. Students 10 and 11 both have age 22.

Constraints

  • 1 ≤ student_data.length ≤ 1000
  • student_data[i].length = 2
  • 1 ≤ student_data[i][0] ≤ 106 (student ID)
  • 1 ≤ student_data[i][1] ≤ 100 (age)

Visualization

Tap to expand
Create DataFrame from 2D ListInput: 2D List[[1, 15], [2, 11], [3, 11], [4, 20]]DataFrame Constructorpd.DataFrame(data, columns)Output: DataFramestudent_id | age---------|----1 | 152 | 113,4... | 11,20...Transform unstructured list data into organized DataFrame with column headersResult: Structured data with labeled columns
Understanding the Visualization
1
Input
2D list with student [ID, age] pairs
2
Transform
Apply DataFrame constructor with column labels
3
Output
Structured DataFrame with named columns
Key Takeaway
🎯 Key Insight: DataFrame constructor efficiently transforms 2D lists into structured data with proper column labels
Asked in
Google 15 Facebook 12 Amazon 18 Microsoft 10
23.0K Views
High Frequency
~5 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen