2877. Create a DataFrame from List
Instructions
- 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
andage
, and be in the same order as the original 2D list. - The result format is in the following example.
Example
Input:
student_data
:
plaintext
[
[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_id
andage
.
Submissions
python
import pandas as pd
def createDataframe(student_data: List[List[int]]) -> pd.DataFrame:
df = pd.DataFrame(student_data, columns=['student_id', 'age'])
return df
Explanation
Python (Pandas)
Submitted by @noeyislearning
import pandas as pd
: Import the pandas library to work with DataFrames.def createDataframe(student_data: List[List[int]]) -> pd.DataFrame:
: Define a function calledcreateDataframe
that takes a 2D liststudent_data
as input and returns a DataFrame.df = pd.DataFrame(student_data, columns=['student_id', 'age'])
: Create a DataFramedf
from the 2D liststudent_data
with columns namedstudent_id
andage
.return df
: Return the DataFramedf
.