AnalystPath

Group Tenants by Shared Rent

PandasMediumMid level~10 min

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.

tenants
tenant_idtenant_namerent
1Dana4000
2Eli4000
3Gil5500
4Hana7000
5Ido7000

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 tenants

Solve 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

Related Pandas questions