Editor

Revenue by Region With pandas

Advanced · Write the code · from 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)

From the lesson: 50 Python Practice Problems with Instant Answer Checking (Beginner to Advanced)

Fifty problems, each with a code editor and real tests. Write your solution, press Check, and see exactly which cases pass. Stuck? Hints reveal one at a time, and the solution unlocks once you have checked at least once. Your code and progress are saved in this browser.

Read the lesson

Each row of a sales DataFrame has region, price and qty. Write revenue_by_region(df) that returns a Series of total revenue (price * qty) per region, sorted from highest to lowest and rounded to 2 decimals. Do not modify df.

import pandas as pd
def revenue_by_region(df):
pass
Solution unlocks after your first Check

What Check runs

Your code runs first, then each of these lines. The output must match exactly.

RunsExpected output
df = pd.DataFrame({ "region": ["North", "South", "North", "East", "South"], "price": [10.0, 20.0, 5.5, 100.0, 1.25], "qty": [3, 2, 10, 1, 4], }) print(revenue_by_region(df).to_dict()){'East': 100.0, 'North': 85.0, 'South': 45.0}

More practice problems

All Python exercises · 50 practice problems