Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab 3 Solutions: Exploratory Analysis with Plotly

Open In Colab

Lab 3 Solutions: Exploratory Analysis with Plotly

These are the worked solutions, with outputs. Try the lab yourself first.

The question. In 2025 Qahwa House timed one order in five in its four branches. The manager wants to print “served within N minutes” on the menu boards of the whole chain. What should N be, and what must change first?

How this lab works

  • Run the cells from top to bottom with Shift + Enter.

  • Guided track: replace every ... with your code, then run the check cell under it. A ✅ means your answer is right.

  • Extended track: after Part D, try the ★ challenges at the end.

  • Submit before the next lab session: File → Download → Download .ipynb, then upload the file to the course page.

You may use an AI assistant for code. If you do, add a short note at the end of the notebook: what you asked, what you kept and what you changed.

Setup

Run the next two cells: the first loads Plotly, the charting library of this lab; the second downloads the data.

(32982, 10)
Loading...
ColumnMeaning
date, weekday, hourwhen the order was paid
branch, citywhere
channelCounter, App or Drive-thru
items, order_valuehow many items, and the amount paid (SAR)
wait_minminutes from payment to the cup in the customer’s hand

Part A: Categorical data

A1. How do customers order?

Use value_counts(normalize=True) on the channel column and multiply by 100 to get percentages. Store the result in channel_pct.

channel Counter 57.9 App 29.2 Drive-thru 12.9 Name: proportion, dtype: float64
✅ A1 percentages add up to 100
✅ A1 Counter is the most common channel (the mode)

A2. Channels by branch

Percentages within each branch: group by branch, then take value_counts(normalize=True) of channel, and .unstack(fill_value=0) to get one row per branch. Store it in mix. Which branches have a drive-thru?

Loading...
✅ A2 one row per branch
✅ A2 University has no drive-thru

Part B: Centre and spread of the wait

B1. Centre

Store the waiting times in w, then compute their mean and median. Which is larger, and why?

4.75 4.2
✅ B1 mean
✅ B1 the mean is above the median (right skew)

B2. Spread

Compute the interquartile range (quantile(0.75) - quantile(0.25)) and the sample standard deviation (.std()).

2.6 2.69
✅ B2 IQR
✅ B2 sample standard deviation (n - 1)

B3. The 90th percentile

Compute the 90th percentile of the waits. Then compute the share of customers who wait longer than the mean. Is it half?

7.7 0.394
✅ B3 90% of orders are at or below the 90th percentile
✅ B3 fewer than half wait longer than the mean

Part C: Pictures with Plotly

plotly.express (imported as px) draws a whole chart in one line. In Colab you can hover over a bar for its value, zoom by dragging, and click a legend entry to hide a group.

C1. A histogram, and its bin width

Run the cell. Then change nbins to 8 and to 200 and run it again each time. Which bin width shows the shape best?

Image produced in Jupyter

C2. Box plots by branch

Draw one box per branch with px.box(orders, x="wait_min", y="branch"). Store the figure in fig_box, then show it. Which branch has the longest tail?

Image produced in Jupyter
✅ C2 a Plotly box plot

C3. Orders per week

Count orders per full week (Sunday to Saturday) and plot them against time. The first and last weeks of the year are partial, so drop them with .iloc[1:-1].

Image produced in Jupyter
✅ C3 51 full weeks

C4. When in the day?

Run the cell: it compares the share of orders per hour on normal days and in Ramadan (1 to 29 March 2025, approximately).

Image produced in Jupyter

Part D: The decision

D1. One row per branch

Build a table promise with one row per branch and four columns: the mean wait, the median, the 90th percentile p90, and over_8, the percentage of orders that wait more than 8 minutes. Sort it by p90.

Loading...
✅ D1 four branches
✅ D1 the right columns
✅ D1 sorted by p90, University last

D2. What would a promise based on the mean cost?

For each branch, the percentage of orders that would break a promise of 5 minutes (about the chain’s mean wait). Store it in broken_at_5.

branch Al Faisaliyah 31.1 Corniche 38.6 Olaya 32.5 University 35.5 Name: wait_min, dtype: float64
✅ D2 a value per branch
✅ D2 more than 30% broken everywhere

D3. Your recommendation

Write three sentences for the manager, who does not read code:

  1. what the data shows, with one or two numbers;

  2. the value of N you recommend, and for which branches;

  3. what must change, or what the data cannot tell us, before the promise is safe everywhere.

Recommendation. A promise based on the average wait would fail: about 5 minutes is the mean, yet 31% to 39% of customers wait longer than that, depending on the branch. A chain-wide promise of 8 minutes is safe in Olaya, Al Faisaliyah and Corniche, where 5% to 9% of orders exceed it, but not at University, where one order in six (16.4%) does, even though its typical customer is served fastest (median 3.8 minutes). I recommend fixing University’s long tail first, with extra staff at its rush hours and in exam months, and launching the 8-minute promise there only once its 90th percentile, now 10.1 minutes, falls below 8; a follow-up timing study would show whether it has.

★ Extended challenges

E1. Rush hours. Compare the median and the 90th percentile of the wait in the rush hours (7, 8, 20 and 21) and at other times. Is the rush a reason to staff differently, or only to promise differently?

E2. Exam months at University. Compare University’s 90th percentile in its exam months (January, May, June, December) with the other months. Does the tail come from the whole year?

E3. The typical basket. Is order_value skewed? Which number would you report as the “typical basket”, and which one would you use to estimate the chain’s annual revenue from this sample?

Loading...

Both the median and the 90th percentile are higher in the rush hours, so the tail is largely made in the rushes. That argues for more staff at those hours rather than a longer promise.

date exam months 11.5 other months 9.4 Name: wait_min, dtype: float64

University’s 90th percentile is clearly higher in exam months, but it stays above the other branches’ even outside them: one barista off-peak makes a long tail all year, and the exam rushes make it worse. Both need fixing before an 8-minute promise.

mean 25.11 SAR, median 23.24 SAR, skewness 1.24
Image produced in Jupyter

The order value is right-skewed too (skewness about 1.2), with two peaks: one-item and two-item baskets. The median (23.24 SAR) describes the typical basket. To estimate annual revenue, though, use the mean: revenue = mean order value × number of orders (here × 5, since one order in five was timed). Totals need the mean; “typical” needs the median.