Compute total sales per region as a pandas Series sorted by region
Quick Overview
Write a pandas function that sums sales for each region and returns a Series named sales, indexed and sorted by region. It tests grouped aggregation without loops, the names and shape of the result, and how missing values change the totals.
Compute total sales per region as a pandas Series sorted by region
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `total_sales_by_region(df)`. The input has a `region` column (str) and a `sales` column (int or float). Return the summed `sales` per `region` as a Series.
```python
import pandas as pd
def total_sales_by_region(df: pd.DataFrame) -> pd.Series:
...
```
Requirements:
- The index is named `region` and the Series is named `sales`.
- Regions are sorted ascending.
- The work is vectorized: no Python loops or `iterrows`.
Example:
```python
df = pd.DataFrame({
'region': ['East', 'West', 'East', 'North'],
'sales': [100, 200, 50, 75],
})
total_sales_by_region(df)
# region
# East 150
# North 75
# West 200
# Name: sales, dtype: int64
```
```hint Shape of the result
Where you select the `sales` column relative to the aggregation decides whether you get a Series named `sales` or a DataFrame.
```
### Constraints & Assumptions
- `region` holds strings.
- `sales` holds integers or floats.
### What a Strong Answer Covers
- A vectorized aggregation with no Python-level loop over rows or regions.
- A result that is a Series named `sales` with an index named `region`.
- Regions in ascending order, and where that order comes from.
- The cost of the operation in terms of rows and distinct regions.
- How missing `sales` values and missing regions are treated.
### Follow-up Questions
- How would you sort by total sales descending instead of by region label?
- What if some `sales` values are NaN?
- How would you return a DataFrame with `region` as a regular column instead of the index?
Overview: Write a pandas function that sums sales for each region and returns a Series named sales, indexed and sorted by region. It tests grouped aggregation without loops, the names and shape of the result, and how missing values change the totals.
Implement total_sales_by_region(df). The input has a region column (str) and a sales column (int or float). Return the summed sales per region as a Series.
import pandas as pd
def total_sales_by_region(df: pd.DataFrame) -> pd.Series:
...
Requirements:
The index is named
region
and the Series is named
sales
.
Regions are sorted ascending.
The work is vectorized: no Python loops or
iterrows
.
Example:
df = pd.DataFrame({
'region': ['East', 'West', 'East', 'North'],
'sales': [100, 200, 50, 75],
})
total_sales_by_region(df)
# region
# East 150
# North 75
# West 200
# Name: sales, dtype: int64
Constraints & Assumptions
region
holds strings.
sales
holds integers or floats.
What a Strong Answer Covers Guidance
A vectorized aggregation with no Python-level loop over rows or regions.
A result that is a Series named
sales
with an index named
region
.
Regions in ascending order, and where that order comes from.
The cost of the operation in terms of rows and distinct regions.
How missing
sales
values and missing regions are treated.
Follow-up Questions Guidance
How would you sort by total sales descending instead of by region label?
What if some
sales
values are NaN?
How would you return a DataFrame with
region
as a regular column instead of the index?