Back to Practice
#0009

Find Revenue By Category

EasyPandas15 min10 XP

Problem

Given a DataFrame with category, price, and quantity columns, return total revenue per category sorted from highest to lowest.

Why This Matters

This is one of the most common data analysis moves: choose the grain, group rows, and aggregate a metric.

Function Signature

def revenue_by_category(df):

Examples

Example 1
Inputcategory: ["book", "pen", "book"], price: [100, 10, 150], quantity: [2, 5, 1]
Outputbook revenue = 350, pen revenue = 50

Book revenue is 100*2 + 150*1. Pen revenue is 10*5.

Constraints

  • Do not modify the original DataFrame in place.
  • Return columns category and revenue.
  • Sort by revenue descending.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 4 hidden categories
Two categories
Inputbook/pen rows with price and quantity
Expectedbook first, then pen

Book has the larger total revenue.

Hidden Test Categories
Single categoryCategories already sorted incorrectlyZero quantity rowsDecimal prices