
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.
1#include <stdio.h>
2#define ROWS 4
3
4void printDataFrame(int student_data[ROWS][2]) {
5 printf("+------------+-----+\n");
6 printf("| student_id | age |\n");
7 printf("+------------+-----+\n");
8 for (int i = 0; i < ROWS; ++i) {
9 printf("| %10d | %3d |\n", student_data[i][0], student_data[i][1]);
10 }
11 printf("+------------+-----+\n");
12}
13
14int main() {
15 int student_data[ROWS][2] = {{1, 15}, {2, 11}, {3, 11}, {4, 20}};
16 printDataFrame(student_data);
17 return 0;
18}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.