
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.
1using System;
2using System.Collections.Generic;
3
4public class Program
5{
6 public static void PrintDataFrame(List<int[]> studentData)
7 {
8 Console.WriteLine("+------------+-----+");
9 Console.WriteLine("| student_id | age |");
10 Console.WriteLine("+------------+-----+");
11 foreach (int[] row in studentData)
12 {
13 Console.WriteLine("| {0,10} | {1,3} |", row[0], row[1]);
14 }
15 Console.WriteLine("+------------+-----+");
16 }
17
18 public static void Main()
19 {
20 List<int[]> studentData = new List<int[]>
21 {
22 new int[] {1, 15},
23 new int[] {2, 11},
24 new int[] {3, 11},
25 new int[] {4, 20}
26 };
27 PrintDataFrame(studentData);
28 }
29}This C# solution uses List<int[]> to store student data and outputs it formatted as a table directly using Console.WriteLine.