AnalystPath

Sites per Region

PandasEasyJunior level~10 min

Problem

A logistics company stores its facilities in a DataFrame `warehouses` with columns `region` and `site`.

For each region, combine all of its sites into a single string separated by a comma and a space (', '), with the site names listed in alphabetical order. Return columns `region` and the combined string aliased as `sites`, ordered by `region`.

Input data

Example rows — the live problem includes the full dataset.

warehouses
regionsite
NorthAberdeen
NorthCarlisle
NorthBradford
SouthBrighton
SouthExeter

Expected output

Your answer should return 3 rows with the columns region, sites.

Starter code (Pandas (Python))

import pandas as pd

def sites_per_region(warehouses) -> pd.DataFrame:
    # Your code here
    return warehouses

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