EasyNo. 2880

Select Data — LeetCode 2880 Python Solution

Topics
Pandas
Reading time
2 min

Table schema

SQLTable schema
DataFrame students +-------------+--------+ | Column Name | Type | +-------------+--------+ | 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.

Example

SQLExample
DataFrame students
+-------------+--------+
| Column Name | Type   |
+-------------+--------+
| student_id  | int    |
| name        | object |
| age         | int    |
+-------------+--------+

Python solution

PythonSolution
import pandas as pd


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

Complexity

Time
O(n)
Space · auxiliary
O(1) to O(n)

Related problems

Frequently asked questions

How hard is LeetCode 2880. Select Data?

LeetCode 2880. Select Data is rated Easy on LeetCode.

What topics does LeetCode 2880. Select Data cover?

LeetCode 2880. Select Data is tagged Pandas on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.