2880 Select Data
Problem Statement
DataFrame students
student_id
int
name
object
age
int
Write a solution to select the name and age of the student with student_id = 101
.
The result format is in the following example.
For the whole problem statement, please refer here.
Plans
Retrieve the student data from the DataFrame students.
Apply a filter to select the row where
student_id
is equal to 101.Select only the
name
andage
columns from the filtered data.Return the resulting DataFrame.
Solution
Explanation
Import Pandas
We start by importing the Pandas library, which provides data structures and operations for manipulating numerical tables and time series.
Define the Function
We define a function selectData that takes a single argument students, which is a DataFrame containing student data.
Filtering Data
We filter the DataFrame students using the condition
students['student_id'] == 101
.This results in a new DataFrame filtered_students which contains only rows with a student_id equal to 101.
Selecting Specific Columns
From the filtered DataFrame, we extract only the name and age columns by using double brackets:
[['name', 'age']]
.
Return the Result
The function returns the DataFrame containing the name and age of the student with
student_id 101
.
Last updated