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 4: Relationships and a First Streamlit App

Open In Colab

Lab 4: Relationships and a First Streamlit App

In this lab you look for relationships in Qahwa House’s orders, then turn your analysis into a small interactive app the manager can use.

The question. The regional manager wants one dashboard to explore the orders, and one answer: which branch and category combination is underperforming, and what should we do about it?

How this lab works

  • Run the cells from top to bottom with Shift + Enter. Replace every ... with your code, then run the check cell under it.

  • Extended track: after Part C, try the ★ challenges.

  • Submit before the next lab session: File → Download → Download .ipynb, plus your qahwa_app.py.

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

ColumnMeaning
order_id, date, weekday, hourthe order and when it was placed
branch, citywhere
app_user1 if the customer ordered through the loyalty app
drinks, food_itemshow many of each in the order
drink_sar, food_sar, basket_saramounts paid in SAR (basket = drinks + food)

Part A: Crosstabs and pivot tables

A1. A crosstab

Count the orders in each combination of branch and app_user with pd.crosstab. Store it in counts.

A2. Row percentages

What share of each branch’s orders come from app users? Use normalize="index" and multiply by 100. Store the app-user column (the column 1) in app_share.

A3. A pivot table

Build a pivot table with one row per branch and the mean of drink_sar, food_sar and basket_sar, plus the share of orders that include food (has_food). Store it in by_branch.

Part B: Relationships

B1. Box plots by branch

Run this cell. Which branch’s baskets are smallest, and which has the most outliers?

B2. Correlation

Compute the correlation matrix of drinks, food_items and basket_sar. Store it in corr. Which of the two drives the basket more strongly?

B3. Do app users spend more?

First compare the mean basket_sar of app users and others overall (store in overall, indexed by app_user), then within each branch with a pivot table (store in within, branches as rows, app_user as columns).

B4. In two sentences: how can both checks above be true? Use your answer to A2.

Your answer here (double-click to edit).

Part C: A first Streamlit app

A Streamlit app is a Python script: each st. call adds an element to a web page (a title, a slider, a table, a chart), and the script runs again from the top every time the user changes a filter.

C1. Fill in the app

The next cell writes the app to a file, qahwa_app.py. Fill in the five gaps marked ..., run the cell, then run the test cell under it.

C2. See it in a browser

Colab cannot show a Streamlit page, so open it in GitHub Codespaces (free with GitHub Education) or on your own computer:

  1. Put qahwa_app.py in a folder, open a terminal there and run pip install streamlit pandas.

  2. Run streamlit run qahwa_app.py; a browser tab opens at http://localhost:8501.

  3. To share it, push the file to a GitHub repository and deploy it on Streamlit Community Cloud (share.streamlit.io): pick the repository and the file, and you get a public link.

The course’s finished version, with more views, is week04/app/app.py in the course repository.

C3. Your recommendation

Use your app: select one branch at a time, and only the food_sar category. Then write three sentences for the manager: which branch × category combination underperforms (with numbers), what you would try, and why the data alone cannot prove the cause.

Write your recommendation here (double-click to edit).

★ Extended challenges

E1. A weekday filter. Add a st.sidebar.multiselect for weekdays to your app and check it still runs with AppTest.

E2. Two correlations, one pair. With the Week 1 sales data, compute the correlation between Iced Latte and Spanish Latte daily units at the Olaya branch, then between their monthly totals. Explain why the sign changes.

E3. An alert. Make your app show st.warning when app users spend more than others in every selected branch but less overall.

AI-use log (if you used an AI assistant)

What you asked, what you kept, what you changed.