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 a DataFrame from List INPUT student_data (2D List) [ [1, 15] [2, 11] [3, 11] [4, 20] ] Index 0: ID Index 1: Age Input Values: Row 0: ID=1, Age=15 Row 1: ID=2, Age=11 Row 2: ID=3, Age=11 Row 3: ID=4, Age=20 ALGORITHM STEPS 1 Import pandas import pandas as pd 2 Define columns ['student_id', 'age'] 3 Create DataFrame pd.DataFrame(data, columns=cols) 4 Return result DataFrame ready! Code: import pandas as pd df = pd.DataFrame( student_data, columns=['student_id', 'age']) FINAL RESULT DataFrame Output student_id age idx 1 15 0 2 11 1 3 11 2 4 20 3 Output Dict Format: {"student_id": [1,2,3,4], "age": [15,11,11,20]} OK - Success! Key Insight: The pd.DataFrame() constructor directly accepts a 2D list and maps each inner list to a row. The columns parameter assigns names to each column, matching the order of elements in each inner list. TutorialsPoint - Create a DataFrame from List | Direct DataFrame Constructor
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