
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.
1import java.util.ArrayList;
2import java.util.List;
3
4public class DataFrame {
5 public static void printDataFrame(List<int[]> student_data) {
6 System.out.println("+------------+-----+");
7 System.out.println("| student_id | age |");
8 System.out.println("+------------+-----+");
9 for (int[] row : student_data) {
10 System.out.format("| %10d | %3d |\n", row[0], row[1]);
11 }
12 System.out.println("+------------+-----+");
13 }
14
15 public static void main(String[] args) {
16 List<int[]> student_data = new ArrayList<>();
17 student_data.add(new int[]{1, 15});
18 student_data.add(new int[]{2, 11});
19 student_data.add(new int[]{3, 11});
20 student_data.add(new int[]{4, 20});
21 printDataFrame(student_data);
22 }
23}In this Java solution, an ArrayList of integer arrays is used to hold the student data. A static method printDataFrame prints the data in the desired format.