Skip to content

2880. Select Data

DataFrame students

Column NameType
student_idint
nameobject
ageint

Instructions

  • Write a solution to select the name and age of the student with student_id = 101.
  • The result format is in the following example.

Example

Input:

student_idnameage
101Ulysses13
53William10
128Henry6
3Henry11

Output:

nameage
Ulysses13

Explanation:

Student Ulysses has student_id = 101, we select the name and age.

Submissions

python
import pandas as pd

def selectData(students: pd.DataFrame) -> pd.DataFrame:
    return students.loc[students['student_id'] == 101, ['name', 'age']]

Explanation

Python (Pandas)
Submitted by @noeyislearning
  • import pandas as pd: Import the pandas library.
  • def selectData(students: pd.DataFrame) -> pd.DataFrame:: Define a function that takes a DataFrame students and returns a DataFrame.
  • return students.loc[students['student_id'] == 101, ['name', 'age']]: Select the rows where student_id is equal to 101 and columns name and age.