1. Lighthouse

Level 1 · Why the average order lies

The request

From Megan Collins, Head of Sales

“The board deck says our average order is about $540, and the new campaign is built around 'customers who spend $540'. That number feels high to me. What does a typical order actually look like?”

Your goal

Set result to a dict with two keys: "mean", the mean of completed["order_value"], and "median", its median, both rounded to 2 decimals.

  • Use the orders DataFramenot done yet
  • Use the order_items DataFramenot done yet
  • Store a dict in resultnot done yet
Completed orders by order value
Completed orders by order value01,0002,0003,000$0 to $100: 2,845$100 to $200: 1,710$200 to $300: 1,201$300 to $400: 933$400 to $500: 670$500 to $600: 528$600 to $700: 413$700 to $800: 356$800 to $900: 287$900 to $1,000: 227$1,000 to $1,100: 195$1,100 to $1,200: 184$1,200 to $1,300: 142$1,300 to $1,400: 131$1,400 to $1,500: 93$1,500 to $1,600: 90$1,600 to $1,700: 130$1,700 to $1,800: 76$1,800 to $1,900: 62$1,900 to $2,000: 49$2,000 to $2,100: 66$2,100 to $2,200: 40$2,200 to $2,300: 42$2,300 to $2,400: 22$2,400 to $2,500: 27$2,500 to $2,600: 28$2,600 to $2,700: 16$2,700 to $2,800: 19$2,800 to $2,900: 22$2,900 to $3,000: 17$0$500$1,000$1,500$2,000$2,500$3,000Order value ($100 bins)Orders
Completed orders by order value01,0002,0003,000$0 to $100: 2,845$100 to $200: 1,710$200 to $300: 1,201$300 to $400: 933$400 to $500: 670$500 to $600: 528$600 to $700: 413$700 to $800: 356$800 to $900: 287$900 to $1,000: 227$1,000 to $1,100: 195$1,100 to $1,200: 184$1,200 to $1,300: 142$1,300 to $1,400: 131$1,400 to $1,500: 93$1,500 to $1,600: 90$1,600 to $1,700: 130$1,700 to $1,800: 76$1,800 to $1,900: 62$1,900 to $2,000: 49$2,000 to $2,100: 66$2,100 to $2,200: 40$2,200 to $2,300: 42$2,300 to $2,400: 22$2,400 to $2,500: 27$2,500 to $2,600: 28$2,600 to $2,700: 16$2,700 to $2,800: 19$2,800 to $2,900: 22$2,900 to $3,000: 17$0$1,000$2,000$3,000Order value ($100 bins)Orders

Each bar counts the orders in a $100 range. The 2% of orders above $3,000 run off to the right, up to $11,103, and are not drawn.

Show the numbers
Order value ($100 bins)Orders
$0 to $1002,845
$100 to $2001,710
$200 to $3001,201
$300 to $400933
$400 to $500670
$500 to $600528
$600 to $700413
$700 to $800356
$800 to $900287
$900 to $1,000227
$1,000 to $1,100195
$1,100 to $1,200184
$1,200 to $1,300142
$1,300 to $1,400131
$1,400 to $1,50093
$1,500 to $1,60090
$1,600 to $1,700130
$1,700 to $1,80076
$1,800 to $1,90062
$1,900 to $2,00049
$2,000 to $2,10066
$2,100 to $2,20040
$2,200 to $2,30042
$2,300 to $2,40022
$2,400 to $2,50027
$2,500 to $2,60028
$2,600 to $2,70016
$2,700 to $2,80019
$2,800 to $2,90022
$2,900 to $3,00017
above $3,000 (not drawn)224

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

    Two kinds of middle

    The mean is the sum of the values divided by how many there are. The median is the value in the middle once you sort them: half the values are below it, half above. On a column without extreme values the two are close. On Harbor & Pine’s order values they are far apart:

    completed["order_value"].mean()     # 543.30
    completed["order_value"].median()   # 268.94

    The mean is twice the median. That gap is the first thing to notice about any column.

    Why the mean moves and the median doesn’t

    The mean uses every value’s size. One order of $11,103 adds as much to the total as forty orders of $270, so a few big orders drag the mean up. The median only cares about order: make the biggest order ten times bigger and the median does not move at all.

    Look at the chart. Most orders sit on the left, under $500, and a long thin tail stretches right to $3,000 and beyond. Data shaped like this is called right-skewed. Money, quantities, session lengths and salaries are almost always right-skewed, which is why the question “mean or median?” comes up in nearly every analysis.

    (completed["order_value"] < completed["order_value"].mean()).mean()   # 0.70

    Seven in ten orders are below the average. A campaign aimed at “customers who spend $540” is aimed at the top 30%.

    Which one to report

    • Typical customer, typical order, typical salary: the median. It describes a real, middle case.
    • Totals and planning: the mean. Revenue = mean order value × number of orders, which is why finance teams use it, and that is correct for their question.
    • Both, side by side, whenever the audience might read one as the other. “Average order $543 (half of orders are under $269)” is one honest line.

    Common mistakes

    • Calling the mean “typical”. On skewed data the mean is pulled toward the tail and describes nobody in particular.
    • Dropping big orders to “fix” the mean. They are real customers. Report the median instead of deleting data you don’t like. (Level 5 covers values that really are errors.)
    • Comparing means of groups with different tails. A segment with a few huge orders can have a higher mean and a lower median than another. Check both before you say one group “spends more”.

    Interview note

    “When would you use the median instead of the mean?” is a standard screening question. The answer they want: when the data is skewed or has outliers, because the median is resistant to extreme values. Add a business example like this one and you have a strong answer.

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