Select Data - Problem

You are given a DataFrame called students with the following structure:

Column NameType
student_idint
nameobject
ageint

Write a solution to select the name and age of the student with student_id = 101.

The result should be a DataFrame containing only the name and age columns for the matching student.

Input & Output

Example 1 — Basic Selection
$ Input: students = [{"student_id": 100, "name": "Alice", "age": 20}, {"student_id": 101, "name": "Bob", "age": 22}, {"student_id": 102, "name": "Charlie", "age": 19}]
Output: [{"name": "Bob", "age": 22}]
💡 Note: Filter for student_id = 101 (Bob) and select only name and age columns
Example 2 — First Student Match
$ Input: students = [{"student_id": 101, "name": "Diana", "age": 21}, {"student_id": 103, "name": "Eve", "age": 23}]
Output: [{"name": "Diana", "age": 21}]
💡 Note: Student 101 is Diana, so return her name and age
Example 3 — No Match Found
$ Input: students = [{"student_id": 100, "name": "Alice", "age": 20}, {"student_id": 102, "name": "Charlie", "age": 19}]
Output: []
💡 Note: No student with ID 101 exists, so return empty result

Constraints

  • 1 ≤ students.length ≤ 1000
  • student_id is unique for each student
  • 1 ≤ age ≤ 100
  • name is a non-empty string

Visualization

Tap to expand
Select Data - Boolean Indexing INPUT student_id name age 100 Alice 20 101 Bob 22 102 Charlie 19 Target: student_id = 101 DataFrame Schema student_id: int name: object age: int Select columns: ['name', 'age'] ALGORITHM STEPS 1 Create Boolean Mask students['student_id']==101 [False, True, False] Index: 0=False, 1=True, 2=False 2 Apply Boolean Index students[mask] 3 Select Columns [['name', 'age']] 4 Return Result DataFrame with 1 row # Complete solution mask = df['student_id']==101 result = df[mask] return result[['name','age']] FINAL RESULT name age Bob 22 Output Format: [{"name": "Bob", "age": 22}] OK - 1 Row Found Result Explanation - Found student with ID 101 - Extracted name: "Bob" - Extracted age: 22 Key Insight: Boolean Indexing is a powerful pandas technique that creates a mask of True/False values. When applied to a DataFrame, only rows where the mask is True are returned. This is more efficient than iterating through rows and is the idiomatic way to filter data in pandas. TutorialsPoint - Select Data | Boolean Indexing Approach
Asked in
Google 25 Amazon 20 Microsoft 15 Netflix 10
23.0K Views
High Frequency
~5 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen