Select a Student’s Name and Age with pandas

Quick Overview

Select the name and age for student_id 101 from a pandas DataFrame. Practice Boolean row filtering, explicit column projection, and preserving the required DataFrame result shape.

Select a Student’s Name and Age with pandas

Company: Amazon

Role: Software Engineer

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

### Problem You receive a pandas DataFrame named `students` with these columns: | Column | Source type | |---|---| | student_id | int | | name | object | | age | int | Implement: ```python def selectData(students: pd.DataFrame) -> pd.DataFrame: ... ``` Select the name and age of the student whose `student_id` equals `101`. Return a DataFrame containing the columns `name` and `age`, in that order, as illustrated below. The target ID is fixed at `101`; it is not another function parameter. ### Examples **Example 1:** Input `students`: | student_id | name | age | |---|---|---| | 42 | Avery | 11 | | 101 | Morgan | 14 | | 9 | Riley | 8 | Returned values: | name | age | |---|---| | Morgan | 14 | The match is determined by `student_id`, not by the row's position. **Example 2:** Input `students`: | student_id | name | age | |---|---|---| | 101 | Casey | 17 | | 103 | Casey | 12 | Returned values: | name | age | |---|---| | Casey | 17 | A matching name alone does not select a row. ### Constraints and Clarifications - Use the `student_id` column value to identify the student. - The result contains `name` followed by `age`, without `student_id`. - Return a DataFrame rather than a Series, scalar, or printed table. - The source does not state a uniqueness guarantee, whether ID `101` must exist, a NULL policy, or an output index or row-order requirement. These must not be silently converted into extra input guarantees. ```hint Separate rows from columns First identify the qualifying row data, then retain the two requested columns. ``` ### What a Strong Answer Covers - Apply equality to the `student_id` column and the fixed value `101`. - Select only the requested output columns in their stated order. - Preserve a DataFrame result instead of collapsing it into a single row object. ### Follow-up Questions - Why is selecting index label `101` different from checking the `student_id` column? - What behavior does your implementation have if there is no match or more than one match, given that the source does not specify those cases?

Overview: Select the name and age for student_id 101 from a pandas DataFrame. Practice Boolean row filtering, explicit column projection, and preserving the required DataFrame result shape.

Amazon logo
Amazon
easySoftware EngineerOtherData Manipulation (SQL/Python)
0
0

Problem

You receive a pandas DataFrame named students with these columns:

ColumnSource type
student_idint
nameobject
ageint

Implement:

def selectData(students: pd.DataFrame) -> pd.DataFrame:
    ...

Select the name and age of the student whose student_id equals 101. Return a DataFrame containing the columns name and age, in that order, as illustrated below. The target ID is fixed at 101; it is not another function parameter.

Examples

Example 1:

Input students:

student_idnameage
42Avery11
101Morgan14
9Riley8

Returned values:

nameage
Morgan14

The match is determined by student_id, not by the row's position.

Example 2:

Input students:

student_idnameage
101Casey17
103Casey12

Returned values:

nameage
Casey17

A matching name alone does not select a row.

Constraints and Clarifications

  • Use the student_id column value to identify the student.
  • The result contains name followed by age , without student_id .
  • Return a DataFrame rather than a Series, scalar, or printed table.
  • The source does not state a uniqueness guarantee, whether ID 101 must exist, a NULL policy, or an output index or row-order requirement. These must not be silently converted into extra input guarantees.

What a Strong Answer Covers Guidance

  • Apply equality to the student_id column and the fixed value 101 .
  • Select only the requested output columns in their stated order.
  • Preserve a DataFrame result instead of collapsing it into a single row object.

Follow-up Questions Guidance

  • Why is selecting index label 101 different from checking the student_id column?
  • What behavior does your implementation have if there is no match or more than one match, given that the source does not specify those cases?