question 426 pandas Transformations · medium
Segment revenue that counts orders twice
segment_revenue should total order revenue per customer segment. Finance noticed the business segment is about twice what the orders add up to. The customers export sometimes lists a customer more than once (identical rows). Fix the function so each order counts once. Orders whose customer isn't listed are left out.
Function
segment_revenue(orders, customers)Returns
{segment: revenue}Examples
callsegment_revenue([{'order_id': 'O100282', 'customer_id': 'C00077', 'total': 35.84}, {'order_id': 'O100376', 'customer_id': 'C00065', 'total': 91.47}, {'order_id': 'O101219', 'customer_id': 'C00077', 'total': 246.6}, {'order_id': 'O101723', 'customer_id': 'C01338', 'total': 54.55}], [{'customer_id': 'C00065', 'segment': 'consumer'}, {'customer_id': 'C00077', 'segment': 'business'}, {'customer_id': 'C00077', 'segment': 'business'}, {'customer_id': 'C01338', 'segment': 'consumer'}])returns{'business': 282.44, 'consumer': 146.02}
callsegment_revenue([{'order_id': 'O100282', 'customer_id': 'C00077', 'total': 35.84}], [{'customer_id': 'C00077', 'segment': 'business'}, {'customer_id': 'C00077', 'segment': 'business'}])returns{'business': 35.84}
callsegment_revenue([], [{'customer_id': 'C00065', 'segment': 'consumer'}, {'customer_id': 'C00077', 'segment': 'business'}, {'customer_id': 'C00077', 'segment': 'business'}, {'customer_id': 'C01338', 'segment': 'consumer'}])returns{}