Total Sales by Region

Each record has region and numeric amount. Group by region with Pandas and sum the amounts, including negative refunds and zero sales. Return a list of dictionaries with region and total, sorted by region in ascending, case-sensitive order. Empty input returns an empty list.

Difficulty: intermediate

Show hint

Group by region, sum the amount column, then reset_index and rename amount to total before converting to records. Construct the named columns for empty input.

Examples

  • [[{"region":"West","amount":10},{"region":"East","amount":7},{"region":"West","amount":5}]][{"region":"East","total":7},{"region":"West","total":15}]

Your solution

Python · Pandas

Results

Python and Pandas load when you Run or Submit. Return a Python list, dictionary or scalar as requested.

Back to Lesson