← All Python interview questions

Compute profit margin per category with groupby

MediumPandas — GroupByDeloitteAccenture

Finance wants a margin per category: total profit divided by total revenue, not the average of the row-level margins.

Write margins(df) that returns a Series indexed by category with the margin as a percentage rounded to 2 decimals, sorted from highest to lowest margin.