Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations

← All challenges
IC06 · Café / orders · ~10–15 min

Understand the category mix

population · frequencies · proportions

Category profile

The question

The morning café team wants to understand its drink mix.

df Category profile

Your inputs

df

One row is one order. Drink labels are already standardised. Proportions are fractions, not percentages.

Field meanings and units
order_id
Order or order-line identifier, as defined by this brief.
drink
Recorded drink label.
size
Recorded serving size.
price
Price in dollars; unparseable text needs the stated policy.
tip
Tip in dollars.
shift
AM or PM service period.
date
Calendar date supplied as YYYY-MM-DD text unless the task states it is invalid.
df · first 5 of 16 rows
order_iddrinksizepricetipshiftdate
C001LatteSmall4.50AM2026-04-01
C002TeaLarge2.81PM2026-04-01
C003LatteSmall4.50.5AM2026-04-02
C004MochaLarge5.2NoneAM2026-04-02
C005LatteSmall4.50PM2026-04-03
View all 16 rows
df · complete input
order_iddrinksizepricetipshiftdate
C001LatteSmall4.50AM2026-04-01
C002TeaLarge2.81PM2026-04-01
C003LatteSmall4.50.5AM2026-04-02
C004MochaLarge5.2NoneAM2026-04-02
C005LatteSmall4.50PM2026-04-03
C006TeaLarge2.81AM2026-04-03
C007LatteSmall4.50.5AM2026-04-04
C008MochaLarge5.2NonePM2026-04-04
C009LatteSmall4.50AM2026-04-05
C010TeaLarge2.81AM2026-04-05
C011LatteSmall4.50.5PM2026-04-06
C012MochaLarge5.2NoneAM2026-04-06
C013LatteSmall4.50AM2026-04-07
C014TeaLarge2.81PM2026-04-07
C015LatteSmall4.50.5AM2026-04-08
C016MochaLarge5.2NoneAM2026-04-08

The editable setup in Your Python creates these inputs when you run it.

Your task · Understand the category mix

The morning café team wants to understand its drink mix.

countsSeriesproportionsSeries

Deliverables

Make the outputs listed below. Use each name exactly as shown. Then choose Check answer.

Python name key

Teal outlines name table columns. Violet left rules name Python objects. Dotted rules mark syntax, functions and parameters. Use each name exactly as shown.

  1. Morning countsOutputcountsSeries
    1. Store the number of AM orders for each drink in counts.
  2. Morning proportionsOutputproportionsSeries
    1. Store each drink’s share of AM orders in proportions, using the same drink labels as counts.
    2. The shares should sum to 1.

Data policy Keep all supplied source tables unchanged. Work on separate outputs.

Help Hints & explained solution

Choose the amount of help you need. Opening one hint does not reveal the others.

Hint 1 — Think

Which orders belong in the denominator?

Hint 2 — Tools

Boolean selection, groupby(), agg(), value_counts() and arithmetic

Revisit: Filter rows · Count categories · Combine conditions · Answer one numerical question · Summarise groups

Hint 3 — Approach

Select AM orders and calculate counts and fractions using that same population.

Explained solution

Add these lines below # Your work in the editor. The supplied input setup is already there.

morning = df[df['shift'] == 'AM']
counts = morning['drink'].value_counts()
proportions = counts / counts.sum()
counts
View supplied setup

This code is already at the top of the editor.

import pandas as pd

# Supplied input: df (16 rows)
df = pd.DataFrame(
    [
        ('C001', 'Latte', 'Small', 4.5, 0, 'AM', '2026-04-01'),
        ('C002', 'Tea', 'Large', 2.8, 1, 'PM', '2026-04-01'),
        ('C003', 'Latte', 'Small', 4.5, 0.5, 'AM', '2026-04-02'),
        ('C004', 'Mocha', 'Large', 5.2, None, 'AM', '2026-04-02'),
        ('C005', 'Latte', 'Small', 4.5, 0, 'PM', '2026-04-03'),
        ('C006', 'Tea', 'Large', 2.8, 1, 'AM', '2026-04-03'),
        ('C007', 'Latte', 'Small', 4.5, 0.5, 'AM', '2026-04-04'),
        ('C008', 'Mocha', 'Large', 5.2, None, 'PM', '2026-04-04'),
        ('C009', 'Latte', 'Small', 4.5, 0, 'AM', '2026-04-05'),
        ('C010', 'Tea', 'Large', 2.8, 1, 'AM', '2026-04-05'),
        ('C011', 'Latte', 'Small', 4.5, 0.5, 'PM', '2026-04-06'),
        ('C012', 'Mocha', 'Large', 5.2, None, 'AM', '2026-04-06'),
        ('C013', 'Latte', 'Small', 4.5, 0, 'AM', '2026-04-07'),
        ('C014', 'Tea', 'Large', 2.8, 1, 'PM', '2026-04-07'),
        ('C015', 'Latte', 'Small', 4.5, 0.5, 'AM', '2026-04-08'),
        ('C016', 'Mocha', 'Large', 5.2, None, 'AM', '2026-04-08'),
    ],
    columns=['order_id', 'drink', 'size', 'price', 'tip', 'shift', 'date'],
)

Why these steps exist

  • Counts and proportions use AM orders only; using all orders as the denominator would understate the morning mix.
  • Each share divides that drink’s count by the total number of AM orders.

Another valid approach

Group counts and normalised value counts are equivalent when they use the same AM orders.

Tab: indent · Shift+Tab: outdent · Esc, then Tab: leave editor

Edit Python. Control or Command plus Enter runs it. Tab indents by four spaces. Shift plus Tab outdents. Press Escape, then Tab or Shift plus Tab to leave the editor.

Each run executes all editor code in a fresh Python session. Named outputs appear below when you run.

Python starts when you open a lesson.

Output

Run your code to see what Python returns.