Group Tenants by Shared Rent
Problem
You are given a DataFrame `tenants` loaded from `tenants.csv`:
```text
+-------------+---------+
| Column | Type |
+-------------+---------+
| tenant_id | int |
| tenant_name | varchar |
| rent | int |
+-------------+---------+
tenant_id is the primary key. Each row gives a tenant's id, name, and monthly rent.
```
A building manager wants to bundle tenants into rent groups for a survey. A group must contain **at least two** tenants, and every tenant in a group pays the **same rent**. All tenants paying the same rent must land in the same group. A tenant whose rent amount is unique belongs to no group.
A group's `tier_id` is the rank of its rent in **ascending** order: the cheapest qualifying rent is tier 1, the next is tier 2, and so on.
Return a DataFrame with columns `tenant_id`, `tenant_name`, `rent`, `tier_id`, ordered by `tier_id` ascending, then `tenant_id` ascending.
Input data
Example rows — the live problem includes the full dataset.
| tenant_id | tenant_name | rent |
|---|---|---|
| 1 | Dana | 4000 |
| 2 | Eli | 4000 |
| 3 | Gil | 5500 |
| 4 | Hana | 7000 |
| 5 | Ido | 7000 |
Expected output
Your answer should return 5 rows with the columns tenant_id, tenant_name, rent, tier_id.
Starter code (Pandas (Python))
import pandas as pd
def group_tenants_by_shared_rent(tenants) -> pd.DataFrame:
# Your code here
return tenantsSolve 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