runtime
424 / 500
question 424 pandas Transformations · easy

Top categories by revenue

A dashboard tile shows the n product categories with the most revenue. Write top_categories(lines, n): add up line_total per category and return the n best categories, highest revenue first; categories with equal revenue are ordered by name, A to Z.

Functiontop_categories(lines, n)
linesdictionaries with category and line_total
Returnsa list of at most n category names
Examples
calltop_categories([{'category': 'sports', 'line_total': 27.9}, {'category': 'fashion', 'line_total': 37.49}, {'category': 'sports', 'line_total': 48}], 1)returns['sports']
calltop_categories([{'category': 'sports', 'line_total': 27.9}, {'category': 'sports', 'line_total': 48}, {'category': 'fashion', 'line_total': 37.49}, {'category': 'grocery', 'line_total': 3.99}, {'category': 'fashion', 'line_total': 42.49}], 2)returns['fashion', 'sports']
calltop_categories([], 3)returns[]

Loading your progress…

Top categories by revenue — Python practice · Runtime