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 1:
Input: student_data:[ [1, 15], [2, 11], [3, 11], [4, 20] ]Output: +------------+-----+ | student_id | age | +------------+-----+ | 1 | 15 | | 2 | 11 | | 3 | 11 | | 4 | 20 | +------------+-----+ Explanation: A DataFrame was created on top of student_data, with two columns namedstudent_idandage.
This approach utilizes the Python pandas library, which is specifically designed for data manipulation and analysis. We will simply pass the list to the DataFrame constructor along with column labels.
This solution uses the pandas library to create a DataFrame. We call the DataFrame constructor with the 2D list and specify the column names. The returned DataFrame has the desired structure.
Time Complexity: O(n) where n is the number of rows in the student_data list.
Space Complexity: O(n) as the data is stored in a DataFrame.
This method manually constructs a DataFrame-like structure using native programming constructs like lists, arrays, dictionaries, etc. This involves manually setting up structures to represent columns and rows and maintaining the desired format.
In this C implementation, we define a function to print the given 2D array in a tabular format. We use standard I/O functions to format each entry like a DataFrame using loops and print statements.
C++
Java
C#
JavaScript
Time Complexity: O(n), where n is the number of rows in the student_data array.
Space Complexity: O(1) since we're modifying and printing directly without additional data structures.
| Approach | Complexity |
|---|---|
| Using pandas library in Python | Time Complexity: O(n) where n is the number of rows in the student_data list. |
| Manual Construction using Native Lists/Arrays | Time Complexity: O(n), where n is the number of rows in the student_data array. |
Creating a Pandas DataFrame From Lists | GeeksforGeeks • GeeksforGeeks • 21,842 views views
Watch 9 more video solutions →Practice Create a DataFrame from List with our built-in code editor and test cases.
Practice on FleetCode