Students and Their Dorm Rooms
Problem
DataFrame: `student` (`student.csv`)
```text
+-------------+--------+
| Column | Type |
+-------------+--------+
| student_id | int |
| given_name | object |
| family_name | object |
+-------------+--------+
student_id uniquely identifies each student.
Each row stores a student's id and name.
```
DataFrame: `room_assignment` (`room_assignment.csv`)
```text
+---------------+--------+
| Column | Type |
+---------------+--------+
| assignment_id | int |
| student_id | int |
| building | object |
| room_no | object |
+---------------+--------+
assignment_id uniquely identifies each assignment.
Each row records the dorm building and room number given to a student.
```
Write a function that returns the `given_name`, `family_name`, `building`, and `room_no` for every student. If a student has not been assigned a room, the `building` and `room_no` should be `null`.
Return the rows in any order.
**Example**
Input — `student`:
```text
student_id given_name family_name
1 Maya Cohen
2 Liam Ortiz
```
Input — `room_assignment`:
```text
assignment_id student_id building room_no
1 2 Maple Hall 204
2 3 Oak Hall 118
```
Output:
```text
given_name family_name building room_no
Maya Cohen None None
Liam Ortiz Maple Hall 204
```
Maya has no room assignment, so her building and room number are `null`. The assignment for `student_id = 3` is dropped because no such student exists.
Input data
Example rows — the live problem includes the full dataset.
| student_id | given_name | family_name |
|---|---|---|
| 1 | Maya | Cohen |
| 2 | Liam | Ortiz |
| assignment_id | student_id | building | room_no |
|---|---|---|---|
| 1 | 2 | Maple Hall | 204 |
| 2 | 3 | Oak Hall | 118 |
Expected output
Your answer should return 2 rows with the columns given_name, family_name, building, room_no.
Starter code (Pandas (Python))
import pandas as pd
def dorm_rooms(student: pd.DataFrame, room_assignment: pd.DataFrame) -> pd.DataFrame:
# Your code here
return studentSolve this Pandas question free
Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock