Filtrar registro

Escribe una solución para seleccionar el nombre y la edad del alumno con student_id = 101.

#pandas#data-selecting#introduction-to-pandas
DataFrame students
+-------------+--------+
| Column Name | Type   |
+-------------+--------+
| student_id  | int    |
| name        | object |
| age         | int    |
+-------------+--------+

El formato del resultado se muestra en el siguiente ejemplo.

Ejemplo 1:

Entrada:

+------------+---------+-----+
| student_id | name    | age |
+------------+---------+-----+
| 101        | Ulysses | 13  |
| 53         | William | 10  |
| 128        | Henry   | 6   |
| 3          | Henry   | 11  |
+------------+---------+-----+

Salida:

+---------+-----+
| name    | age | 
+---------+-----+
| Ulysses | 13  |
+---------+-----+

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

Solución

import pandas as pd


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

slackmart blog © 2024