Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations

← All challenges
WC09 · Measurements / activity · ~10–20 min

Make repeated measurements usable

data shape · units · selection

Long measurements

The question

Prepare an activity extract that keeps each value connected to its measurement and unit.

df Long measurements

Your inputs

df

The measure label carries the unit: steps is a count, minutes is elapsed time. Never combine their numeric values into one total.

Field meanings and units
record_id
Activity-observation identifier.
group
Comparison group.
date
Calendar date supplied as YYYY-MM-DD text unless the task states it is invalid.
steps
Recorded step count, additive across observations.
minutes
Elapsed duration in minutes; unknown is not zero.
week
Current or Previous reporting week.
df · first 5 of 12 rows
record_idgroupdatestepsminutesweek
A001Morning2026-04-01250020Previous
A002Evening2026-04-01307027Previous
A003Morning2026-04-02364034Previous
A004Evening2026-04-02421041Previous
A005Morning2026-04-03478048Previous
View all 12 rows
df · complete input
record_idgroupdatestepsminutesweek
A001Morning2026-04-01250020Previous
A002Evening2026-04-01307027Previous
A003Morning2026-04-02364034Previous
A004Evening2026-04-02421041Previous
A005Morning2026-04-03478048Previous
A006Evening2026-04-03535055Previous
A007Morning2026-04-04592021Previous
A008Evening2026-04-04264028Previous
A009Morning2026-04-05321035Previous
A010Evening2026-04-05378042Previous
A011Morning2026-04-06435049Previous
A012Evening2026-04-06492056Previous

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

Your task · Make repeated measurements usable

Prepare an activity extract that keeps each value connected to its measurement and unit.

longDataFrameduration_recordsDataFrame

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. One row per measurementOutputlongDataFrame
    1. Create long with record_id, measure and value, using steps and minutes only.
    2. Include both measurements for every source record, including unknown values.
  2. Duration extractOutputduration_recordsDataFrame
    1. Create duration_records from long for measure minutes and value at least 35.

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

How will a reader know whether a value counts steps or measures minutes?

Hint 2 — Tools

melt() and boolean selection

Revisit: Wide → long · Combine conditions

Hint 3 — Approach

Retain the record identifier while stacking the two measures, then select qualifying duration observations.

Explained solution

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

long = df.melt(
    id_vars=['record_id'],
    value_vars=['steps', 'minutes'],
    var_name='measure',
    value_name='value',
)
duration_records = long[(long.measure == 'minutes') & (long.value >= 35)]
long
View supplied setup

This code is already at the top of the editor.

import pandas as pd

# Supplied input: df (12 rows)
df = pd.DataFrame(
    [
        ('A001', 'Morning', '2026-04-01', 2500, 20, 'Previous'),
        ('A002', 'Evening', '2026-04-01', 3070, 27, 'Previous'),
        ('A003', 'Morning', '2026-04-02', 3640, 34, 'Previous'),
        ('A004', 'Evening', '2026-04-02', 4210, 41, 'Previous'),
        ('A005', 'Morning', '2026-04-03', 4780, 48, 'Previous'),
        ('A006', 'Evening', '2026-04-03', 5350, 55, 'Previous'),
        ('A007', 'Morning', '2026-04-04', 5920, 21, 'Previous'),
        ('A008', 'Evening', '2026-04-04', 2640, 28, 'Previous'),
        ('A009', 'Morning', '2026-04-05', 3210, 35, 'Previous'),
        ('A010', 'Evening', '2026-04-05', 3780, 42, 'Previous'),
        ('A011', 'Morning', '2026-04-06', 4350, 49, 'Previous'),
        ('A012', 'Evening', '2026-04-06', 4920, 56, 'Previous'),
    ],
    columns=['record_id', 'group', 'date', 'steps', 'minutes', 'week'],
)

Why these steps exist

  • The identifier is repeated beside each measurement so values retain their source record.
  • Filtering on both measure and value prevents a large step count from being mistaken for a long duration.

Another valid approach

Equivalent reshaping is accepted if every requested record and measurement pair appears once with the correct value.

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.