Sum sales by region and category into a MultiIndex Series in pandas
Quick Overview
Write a pandas function that totals sales for every region and category pair and returns a Series with a two-level MultiIndex named sales. It tests grouping on several columns, the index and name of the result, sort order, and how missing values are treated.
Sum sales by region and category into a MultiIndex Series in pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `sales_by_region_and_category(df)`. The input DataFrame has columns `region`, `category` and `sales`. Return the total `sales` for each (`region`, `category`) pair.
The result must meet these requirements:
- Return a Series with a two-level MultiIndex (`region`, `category`), named `sales`.
- Sort it by `region`, then `category`, ascending.
For example:
```python
df = pd.DataFrame({
'region': ['E', 'E', 'W', 'W', 'E'],
'category': ['A', 'B', 'A', 'A', 'A'],
'sales': [10, 5, 3, 7, 4],
})
sales_by_region_and_category(df)
# region category
# E A 14
# B 5
# W A 10
# Name: sales, dtype: int64
```
```hint Two keys at once
`groupby` accepts more than one column. Think about what index the result gets when you group on two keys, and where its name comes from.
```
### What a Strong Answer Covers
- How every distinct (`region`, `category`) combination is aggregated in one pass.
- The shape of the result: the MultiIndex levels and the Series name.
- How the required sort order is guaranteed.
- What happens with missing values in the key columns or in `sales`.
### Follow-up Questions
- How would you return a DataFrame with `region` and `category` as columns instead of an index?
- How would you pivot this into a region-by-category matrix?
Overview: Write a pandas function that totals sales for every region and category pair and returns a Series with a two-level MultiIndex named sales. It tests grouping on several columns, the index and name of the result, sort order, and how missing values are treated.
Implement sales_by_region_and_category(df). The input DataFrame has columns region, category and sales. Return the total sales for each (region, category) pair.
The result must meet these requirements:
Return a Series with a two-level MultiIndex (
region
,
category
), named
sales
.
Sort it by
region
, then
category
, ascending.
For example:
df = pd.DataFrame({
'region': ['E', 'E', 'W', 'W', 'E'],
'category': ['A', 'B', 'A', 'A', 'A'],
'sales': [10, 5, 3, 7, 4],
})
sales_by_region_and_category(df)
# region category
# E A 14
# B 5
# W A 10
# Name: sales, dtype: int64
What a Strong Answer Covers Guidance
How every distinct (
region
,
category
) combination is aggregated in one pass.
The shape of the result: the MultiIndex levels and the Series name.
How the required sort order is guaranteed.
What happens with missing values in the key columns or in
sales
.
Follow-up Questions Guidance
How would you return a DataFrame with
region
and
category
as columns instead of an index?
How would you pivot this into a region-by-category matrix?