1. Lighthouse

Level 1 · Do pricier products sell less?

The request

From Jessica Turner, Merchandising Manager

“Before we reprice, I'd like a number for something we all believe: expensive products sell fewer units. How strong is that relationship across our catalog, really?”

Your goal

Set result to a dict with two keys, both rounded to 3 decimals: "pearson", the correlation between per_product["unit_price"] and per_product["units_sold"], and "rank", the correlation between their ranks (.rank() of each column, then .corr()).

  • Use the products DataFramenot done yet
  • Use the orders DataFramenot done yet
  • Store a dict in resultnot done yet
Units sold by list price, one dot per product
Units sold by list price, one dot per product05001,0001,5002,0002,500$0$500$1,000$1,500$2,000$31.99, 571$11.99, 1,568$159, 787$19.99, 2,320$25.99, 812$53.99, 1,349$92.99, 507$349, 309$20.99, 2,017$159, 559$66.99, 96$70.99, 423$1,599, 163$28.99, 2,300$239, 293$17.99, 1,704$149, 214$179, 384$189, 606$27.99, 842$25.99, 601$76.99, 433$259, 370$104.99, 1,325$189, 568$13.99, 1,035$289, 205$169, 592$53.99, 772$159, 190$45.99, 687$179, 268$379, 1,046$38.99, 664$54.99, 648$179, 354$109, 545$43.99, 1,122$179, 766$189, 629$109, 324$369, 101$72.99, 467$29.99, 1,318$93.99, 747$409, 113$18.99, 402$10.99, 1,527$329, 139$84.99, 360$179, 195$449, 152$30.99, 546$199, 305$52.99, 1,645$50.99, 716$259, 143$78.99, 180$219, 317$129, 196$86.99, 83$289, 514$23.99, 517$249, 331$629, 98$439, 79$79.99, 206$74.99, 509$779, 37$429, 39$1,099, 208$51.99, 238$1,379, 33$779, 88$1,609, 105$689, 56$219, 64$969, 45$669, 53$169, 115$349, 465$13.99, 743$789, 37$16.99, 853$219, 114$72.99, 169$1,659, 44$60.99, 792$999, 208$1,209, 25$109, 105$169, 492$379, 34$15.99, 454$179, 100$239, 54$19.99, 1,061$919, 46$20.99, 744$229, 377$30.99, 226$17.99, 295$189, 167$519, 77$159, 71$44.99, 790$51.99, 211$70.99, 356$139, 138$139, 447$289, 23$12.99, 407$109, 135$279, 29$279, 8$18.99, 195$1,289, 6$17.99, 305$103.99, 35$869, 13List priceUnits sold, 2023–2025
Units sold by list price, one dot per product05001,0001,5002,0002,500$0$500$1,000$1,500$2,000$31.99, 571$11.99, 1,568$159, 787$19.99, 2,320$25.99, 812$53.99, 1,349$92.99, 507$349, 309$20.99, 2,017$159, 559$66.99, 96$70.99, 423$1,599, 163$28.99, 2,300$239, 293$17.99, 1,704$149, 214$179, 384$189, 606$27.99, 842$25.99, 601$76.99, 433$259, 370$104.99, 1,325$189, 568$13.99, 1,035$289, 205$169, 592$53.99, 772$159, 190$45.99, 687$179, 268$379, 1,046$38.99, 664$54.99, 648$179, 354$109, 545$43.99, 1,122$179, 766$189, 629$109, 324$369, 101$72.99, 467$29.99, 1,318$93.99, 747$409, 113$18.99, 402$10.99, 1,527$329, 139$84.99, 360$179, 195$449, 152$30.99, 546$199, 305$52.99, 1,645$50.99, 716$259, 143$78.99, 180$219, 317$129, 196$86.99, 83$289, 514$23.99, 517$249, 331$629, 98$439, 79$79.99, 206$74.99, 509$779, 37$429, 39$1,099, 208$51.99, 238$1,379, 33$779, 88$1,609, 105$689, 56$219, 64$969, 45$669, 53$169, 115$349, 465$13.99, 743$789, 37$16.99, 853$219, 114$72.99, 169$1,659, 44$60.99, 792$999, 208$1,209, 25$109, 105$169, 492$379, 34$15.99, 454$179, 100$239, 54$19.99, 1,061$919, 46$20.99, 744$229, 377$30.99, 226$17.99, 295$189, 167$519, 77$159, 71$44.99, 790$51.99, 211$70.99, 356$139, 138$139, 447$289, 23$12.99, 407$109, 135$279, 29$279, 8$18.99, 195$1,289, 6$17.99, 305$103.99, 35$869, 13List priceUnits sold, 2023–2025

The dashed line is the best straight line through the dots. The dots bend: steep at low prices, nearly flat above $500.

Show the numbers
List priceUnits sold, 2023–2025
$31.99571
$11.991,568
$159787
$19.992,320
$25.99812
$53.991,349
$92.99507
$349309
$20.992,017
$159559
$66.9996
$70.99423
$1,599163
$28.992,300
$239293
$17.991,704
$149214
$179384
$189606
$27.99842
$25.99601
$76.99433
$259370
$104.991,325
$189568
$13.991,035
$289205
$169592
$53.99772
$159190
$45.99687
$179268
$3791,046
$38.99664
$54.99648
$179354
$109545
$43.991,122
$179766
$189629
$109324
$369101
$72.99467
$29.991,318
$93.99747
$409113
$18.99402
$10.991,527
$329139
$84.99360
$179195
$449152
$30.99546
$199305
$52.991,645
$50.99716
$259143
$78.99180
$219317
$129196
$86.9983
$289514
$23.99517
$249331
$62998
$43979
$79.99206
$74.99509
$77937
$42939
$1,099208
$51.99238
$1,37933
$77988
$1,609105
$68956
$21964
$96945
$66953
$169115
$349465
$13.99743
$78937
$16.99853
$219114
$72.99169
$1,65944
$60.99792
$999208
$1,20925
$109105
$169492
$37934
$15.99454
$179100
$23954
$19.991,061
$91946
$20.99744
$229377
$30.99226
$17.99295
$189167
$51977
$15971
$44.99790
$51.99211
$70.99356
$139138
$139447
$28923
$12.99407
$109135
$27929
$2798
$18.99195
$1,2896
$17.99305
$103.9935
$86913

Your code

Python

Loading the editor…

    ResultPreview of products · press Run to see yours
    Preview of products
    product_idnamecategorysubcategoryunit_price
    101Vale Velvet LoveseatFurnitureSofas1659.00
    102Alder Oak Coffee TableFurnitureTables919.00
    103Orchard Rattan Dining ChairFurnitureChairs789.00
    104Upland Oak BookcaseFurnitureStorage429.00

    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.

    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
    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
    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

    What a correlation measures

    The correlation coefficient r is a number from -1 to 1 that says how closely two columns move along a straight line:

    • close to 1: when one is high, the other is high, almost on a rising line;
    • close to -1: when one is high, the other is low, almost on a falling line;
    • close to 0: no straight-line pattern.

    In pandas it is one call on two Series of the same length:

    per_product["unit_price"].corr(per_product["units_sold"])   # -0.425

    Across Harbor & Pine’s 120 products, r is -0.425: pricier products sell fewer units, but the points are far from a tidy line. A common rule of thumb calls 0.1 weak, 0.3 moderate and 0.5 strong, but the number alone never tells you the shape. Look at the chart.

    When Pearson undersells a curve

    Pearson’s r (the default .corr()) rewards a straight line. The dots in the chart bend: units fall steeply from $11 bowl sets and vases to $200 lamps, then barely change from $800 dining chairs to the $1,659 loveseat. The dashed best-fit line misses both ends, so r looks only moderate.

    A rank correlation (Spearman’s) asks a looser question: as price goes up, do units go down, whatever the shape? Replace each value with its rank (1 = cheapest) and correlate the ranks:

    per_product["unit_price"].rank().corr(per_product["units_sold"].rank())   # -0.694

    -0.694 is a strong negative relationship. With SciPy installed, .corr(method="spearman") gives the same number; ranking first works everywhere, including this browser.

    What r does not tell you

    • Not the slope. r = -0.7 does not mean “each extra dollar costs 0.7 units”. The size of the effect is a regression question (level 3).
    • Not the cause. The cheap end is kitchenware, decor and textiles, which far more customers shop for; the expensive end is furniture and outdoor sets. Price is tangled up with what the product is, so this chart cannot say what a price cut would do to units.
    • Not robust to outliers. One product with huge sales can move r a lot. Ranks are much less sensitive.

    Common mistakes

    • Reporting r without the scatter plot. Very different shapes can share the same r.
    • Calling a moderate r “no relationship”. Here r = -0.43 hides a strong, curved pattern.
    • Mixing grains. Correlating price with units per order line instead of per product counts popular products hundreds of times. Decide what one row is (here: one product), build both columns in one DataFrame at that grain, then correlate.

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