Create a DataFrame from List - Problem

You're a data scientist who just received student information in the form of a 2D list. Your task is to transform this raw data into a properly structured pandas DataFrame for further analysis.

Given a 2D list called student_data where each inner list contains exactly two elements:

  • The first element is the student ID
  • The second element is the student's age

Create a DataFrame with two columns: student_id and age. The DataFrame should preserve the exact same order as the original 2D list.

Example:

Input: [[1, 15], [2, 11], [3, 11], [4, 20]]

Output: A DataFrame with columns 'student_id' and 'age' containing the respective values.

Input & Output

example_1.py โ€” Basic Student Data
$ Input: student_data = [[1, 15], [2, 11], [3, 11], [4, 20]]
โ€บ Output: student_id age 0 1 15 1 2 11 2 3 11 3 4 20
๐Ÿ’ก Note: The 2D list is converted to a DataFrame where the first column becomes 'student_id' and the second column becomes 'age', maintaining the original order.
example_2.py โ€” Single Student
$ Input: student_data = [[100, 18]]
โ€บ Output: student_id age 0 100 18
๐Ÿ’ก Note: Even with a single student, the DataFrame is created with the same column structure.
example_3.py โ€” Mixed Ages
$ Input: student_data = [[5, 25], [10, 30], [15, 22]]
โ€บ Output: student_id age 0 5 25 1 10 30 2 15 22
๐Ÿ’ก Note: The function handles various student IDs and ages, preserving the exact order from the input list.

Visualization

Tap to expand
Raw 2D List[1, 15][2, 11][3, 11][4, 20]Unstructured DataPattern RecognitionRow = [id, age]Column mappingAdd Column Namesstudent_id | ageSemantic meaningStructured DataFramestudent_idage115211311Ready for Analysis!AnalyzeLabelStructure๐Ÿ“ŠMessy Dataโœ…Clean DataFrame
Understanding the Visualization
1
Raw Input
2D list with student information scattered as nested arrays
2
Structure Recognition
Pandas identifies the pattern: each sub-list represents a row, elements represent column values
3
Column Assignment
Apply meaningful column names 'student_id' and 'age' to give context to the data
4
DataFrame Creation
Final organized structure ready for data analysis and manipulation
Key Takeaway
๐ŸŽฏ Key Insight: Pandas DataFrame constructor automatically handles the conversion from 2D lists to structured data when provided with appropriate column names, making data transformation seamless and efficient.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Pandas internally processes each row once to create the DataFrame, where n is the number of students

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Space is used to store the DataFrame structure and data, no additional intermediate storage needed

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค len(student_data) โ‰ค 103
  • Each inner list contains exactly 2 elements: [student_id, age]
  • 1 โ‰ค student_id โ‰ค 106
  • 1 โ‰ค age โ‰ค 150
  • student_data is guaranteed to be a valid 2D list
Asked in
Google 25 Amazon 18 Meta 15 Netflix 12
28.0K Views
High Frequency
~5 min Avg. Time
842 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