
Sponsored
Sponsored
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.
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.
1import pandas as pd
2
3def create_dataframe(student_data):
4 df
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.
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.
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.
1function printDataFrame(student_data) {
2 console.log('+------------+-----+');
3 console.log('| student_id | age |');
4 console.log('+------------+-----+');
5 student_data.forEach(row => {
6 console.log(`| ${row[0].toString().padStart(10)} | ${row[1].toString().padStart(3)} |`);
7 });
8 console.log('+------------+-----+');
9}
10
11const student_data = [
12 [1, 15],
13 [2, 11],
14 [3, 11],
15 [4, 20]
16];
17
18printDataFrame(student_data);This JavaScript solution utilizes the console.log method to display the data formatted as a DataFrame, iterating through the data using forEach.