Compute each row's percentage of its region's sales total in pandas
Quick Overview
Write a pandas function that adds each row's sales as a percentage of its region's total, keeping the original index and row order and leaving the input frame untouched. It tests grouped aggregation that maps back onto rows, index alignment and zero-total edge cases.
Compute each row's percentage of its region's sales total in pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `add_pct_of_group_total(df)`. The input DataFrame has columns `region` and `sales`. Return a copy with a float column `pct_of_region` giving each row's `sales` as a percentage of its `region`'s total.
Requirements:
- `pct_of_region = 100 * sales / (group sum of sales)` per `region`.
- Preserve the original index and row order.
- Do not mutate the input DataFrame.
Example:
```python
df = pd.DataFrame({'region': ['N', 'N', 'S'], 'sales': [30, 10, 50]})
add_pct_of_group_total(df)
# region sales pct_of_region
# 0 N 30 75.0
# 1 N 10 25.0
# 2 S 50 100.0
```
Region N totals 40, so its rows get 75.0 and 25.0. Region S has a single row, which holds all of its sales.
```hint Shape of the group total
Each row needs its region's total repeated next to it. Look for a grouped operation whose output has the same length and index as the input.
```
### What a Strong Answer Covers
- Bringing each region's total back onto its own rows without collapsing or reordering the frame
- Keeping the original index and row order in the output
- Returning a new frame and leaving the caller's DataFrame unchanged
- Producing a float column, including when `sales` holds integers
- What happens for a region whose total is 0 and for missing values in `sales` or `region`
### Follow-up Questions
- Why use `transform('sum')` instead of `groupby(...).sum()` with a merge?
- How would a `region` whose total `sales` is 0 behave, and how would you guard against it?
- How would you compute each row's share within its region and month instead?
- What happens to rows whose `region` is missing?
Overview: Write a pandas function that adds each row's sales as a percentage of its region's total, keeping the original index and row order and leaving the input frame untouched. It tests grouped aggregation that maps back onto rows, index alignment and zero-total edge cases.
Implement add_pct_of_group_total(df). The input DataFrame has columns region and sales. Return a copy with a float column pct_of_region giving each row's sales as a percentage of its region's total.
Requirements:
pct_of_region = 100 * sales / (group sum of sales)
per
region
.
Preserve the original index and row order.
Do not mutate the input DataFrame.
Example:
df = pd.DataFrame({'region': ['N', 'N', 'S'], 'sales': [30, 10, 50]})
add_pct_of_group_total(df)
# region sales pct_of_region
# 0 N 30 75.0
# 1 N 10 25.0
# 2 S 50 100.0
Region N totals 40, so its rows get 75.0 and 25.0. Region S has a single row, which holds all of its sales.
What a Strong Answer Covers Guidance
Bringing each region's total back onto its own rows without collapsing or reordering the frame
Keeping the original index and row order in the output
Returning a new frame and leaving the caller's DataFrame unchanged
Producing a float column, including when
sales
holds integers
What happens for a region whose total is 0 and for missing values in
sales
or
region
Follow-up Questions Guidance
Why use
transform('sum')
instead of
groupby(...).sum()
with a merge?
How would a
region
whose total
sales
is 0 behave, and how would you guard against it?
How would you compute each row's share within its region and month instead?