1. Lighthouse

Level 4 · Which orders are unusually large?

The request

From Laura Bennett, Head of Finance

“The auditors want to know which 2025 orders are 'unusually large' and how we define that. Give me a rule I can defend, the cutoff in dollars and how many orders are above it.”

Your goal

Using order lines with quantity > 0 from orders with status 'completed' placed in 2025, compute each order's value (sum of quantity * unit_price * (1 - discount_pct / 100)). Set result to a dict with keys q1, q3 (the 25th and 75th percentiles of order value), upper_fence (q3 + 1.5 * (q3 - q1)), all rounded to 2, and n_outliers (the number of orders with a value above upper_fence, an int).

  • Use the orders DataFramenot done yet
  • Use the order_items DataFramenot done yet
  • Store a dict in resultnot done yet

Your code

Python

Loading the editor…

    ResultPreview of orders · press Run to see yours
    Preview of orders
    order_idcustomer_idorder_datestatuschannel
    10011252023-01-01 20:51:19completedweb
    10021562023-01-02 07:58:46completedweb
    1003222023-01-02 10:25:27completedmarketplace
    1004482023-01-02 11:21:51completedmarketplace

    Mote checks your result

    Press Run. Each row that matches the goal becomes a firefly in your lantern.

    DataFrames

    Loaded for you as pandas DataFrames, plus pd and np.

    orders12,000 rows · 7 columns
    • order_idinteger
    • customer_idinteger
    • order_datetimestamp
    • statusvarchar
    • channelvarchar
    • shipping_feedecimal
    • coupon_codevarchar
    First 5 rows of orders
    order_idcustomer_idorder_datestatuschannelshipping_feecoupon_code
    10011252023-01-01 20:51:19completedweb0.00NaN
    10021562023-01-02 07:58:46completedweb0.00NaN
    1003222023-01-02 10:25:27completedmarketplace4.99WELCOME10
    1004482023-01-02 11:21:51completedmarketplace4.99WELCOME10
    1005822023-01-02 11:43:36completedweb0.00WELCOME10
    order_items30,338 rows · 6 columns
    • order_item_idinteger
    • order_idinteger
    • product_idinteger
    • quantityinteger
    • unit_pricedecimal
    • discount_pctdecimal
    First 5 rows of order_items
    order_item_idorder_idproduct_idquantityunit_pricediscount_pct
    11001121931.990.00
    21001154311.990.00
    31002154411.990.00
    410021941149.000.00
    51002193219.990.00
    customers2,018 rows · 9 columns
    • customer_idinteger
    • first_namevarchar
    • last_namevarchar
    • emailvarchar
    • cityvarchar
    • statevarchar
    • signup_datedate
    • segmentvarchar
    • referral_sourcevarchar
    First 5 rows of customers
    customer_idfirst_namelast_nameemailcitystatesignup_datesegmentreferral_source
    1OwenHudsonowen.hudson@icloud.comSan FranciscoCA2022-06-02consumersearch
    2DouglasMendozadmendoza69@gmail.comAtlantaGA2022-06-02consumerNaN
    3HenryBrookshbrooks@lakesidecafe.comLos AngelesCA2022-06-04businessNaN
    4BrianGrahambgraham@bluecedarhomes.comSeattleWA2022-06-06businesspinterest
    5KyleJimenezkjimenez27@yahoo.comIndianapolisIN2022-06-07consumerNaN
    products120 rows · 8 columns
    • product_idinteger
    • namevarchar
    • categoryvarchar
    • subcategoryvarchar
    • unit_pricedecimal
    • unit_costdecimal
    • launched_ondate
    • is_activeboolean
    First 5 rows of products
    product_idnamecategorysubcategoryunit_priceunit_costlaunched_onis_active
    101Vale Velvet LoveseatFurnitureSofas1659.00NaN2023-09-11true
    102Alder Oak Coffee TableFurnitureTables919.00503.502024-05-16true
    103Orchard Rattan Dining ChairFurnitureChairs789.00429.872023-05-27true
    104Upland Oak BookcaseFurnitureStorage429.00214.702022-12-20true
    105Avery Leather Bed FrameFurnitureBeds449.00232.702021-08-31true
    returns1,673 rows · 5 columns
    • return_idinteger
    • order_item_idinteger
    • returned_ondate
    • reasonvarchar
    • refund_amountdecimal
    First 5 rows of returns
    return_idorder_item_idreturned_onreasonrefund_amount
    1142023-01-10damaged83.69
    2422023-01-10wrong_item19.99
    31052023-01-14wrong_item10.99
    4242023-01-16changed_mind26.99
    51262023-01-16late20.99
    employees60 rows · 8 columns
    • employee_idinteger
    • first_namevarchar
    • last_namevarchar
    • departmentvarchar
    • titlevarchar
    • manager_idinteger
    • hire_datedate
    • salaryinteger
    First 5 rows of employees
    employee_idfirst_namelast_namedepartmenttitlemanager_idhire_datesalary
    1CatherineHaleExecutiveChief Executive OfficerNaN2016-03-01285000
    2RobertKimExecutiveChief Operating Officer12017-01-09235000
    3MeganCollinsSalesHead of Sales12018-04-16172000
    4ChloeGutierrezSalesAccount Executive32020-01-2172000
    5FrankBellSalesAccount Executive32023-05-2173000
    web_sessions40,000 rows · 7 columns
    • session_idinteger
    • customer_idinteger
    • started_attimestamp
    • devicevarchar
    • sourcevarchar
    • pages_viewedinteger
    • convertedboolean
    First 5 rows of web_sessions
    session_idcustomer_idstarted_atdevicesourcepages_viewedconverted
    1NaN2023-01-01 23:02:24mobilepaid1false
    2182023-01-01 15:07:49desktoppaid1false
    3NaN2023-01-01 16:55:46desktopdirect7false
    4NaN2023-01-01 12:43:28mobileorganic3false
    5NaN2023-01-01 13:06:21mobilesocial2false
    marketing_spend3,276 rows · 3 columns
    • spend_datedate
    • channelvarchar
    • amountdecimal
    First 5 rows of marketing_spend
    spend_datechannelamount
    2023-01-01paid492.99
    2023-01-01email37.38
    2023-01-01social200.53
    2023-01-02paid709.13
    2023-01-02email64.13

    The lesson

    A rule instead of a feeling

    “Unusually large” needs a definition you can write down and repeat. The usual one is the 1.5 × IQR rule, the same rule a boxplot uses for its whiskers:

    • compute Q1 and Q3, the 25th and 75th percentiles;
    • the IQR is Q3 - Q1, the spread of the middle half;
    • the upper fence is Q3 + 1.5 × IQR, the lower fence Q1 - 1.5 × IQR;
    • values beyond a fence are outliers.

    It works on skewed data like order values because quartiles, unlike the mean and the standard deviation, aren’t dragged around by the extremes they’re trying to detect.

    In pandas

    quantile accepts a list and returns one value per fraction, which unpacks neatly:

    q1, q3 = order_value.quantile([0.25, 0.75])   # 87.94, 650.48
    iqr = q3 - q1                                    # 562.53
    upper = q3 + 1.5 * iqr                           # 1494.27
    lower = q1 - 1.5 * iqr                           # negative: no low outliers possible
    outliers = order_value[order_value > upper]
    len(outliers)                                    # 370

    Out of 4,295 completed 2025 orders, 370 are above $1,494.27, about 8.6%. That’s more than you’d see in a symmetric distribution, which is the long right tail again.

    A seaborn boxplot of a single Series shows the same thing: sns.boxplot(x=order_value). Every dot to the right of the whisker is one of those 370 orders, and an axvline at the fence makes the rule visible.

    Outlier is not the same as wrong

    The next step is to look at who the outliers are, never to delete them on sight. Joining the segment shows that 256 of the 370 come from business accounts, which make up about a quarter of all orders. They’re unusual, and they’re some of the most valuable orders the store has. Dropping them would cut a large slice of revenue out of every total.

    What you do depends on the question. For “what does a typical order look like”, report the median, which isn’t moved by them anyway. For revenue, keep them. For a model that would be thrown off by them, cap or segment them, and write down that decision and why. An outlier that is a real error, like a negative quantity, is a cleaning problem with its own rule.

    Common mistakes

    • Using quantile(25): quantile takes fractions, 0.25 and 0.75.
    • Computing the fences on order lines instead of orders.
    • Dropping outliers without checking what they are.
    • Forgetting the lower fence exists. Here it’s negative, so it flags nothing, but say so.

    Pick what may run on this site. You can change this any time from "Cookie settings" at the bottom of every page.