Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations

← All challenges
IC08 · Student attendance / study · ~10–15 min

Compare groups fairly

population · comparison · summaries

Group comparison

The question

Compare study groups using attendees with known scores only.

df Group comparison

Your inputs

df

One row is one learner’s submission. Attendance and a known score are required; study hours may be unknown.

Field meanings and units
submission_id
Attendance-submission identifier.
student
Learner label.
group
Comparison group.
hours
Study duration in hours.
score
Assessment score in points.
attended
Whether the learner attended.
date
Calendar date supplied as YYYY-MM-DD text unless the task states it is invalid.
df · first 5 of 18 rows
submission_idstudentgrouphoursscoreattendeddate
S001Learner 1A255false2026-04-01
S002Learner 2B578true2026-04-01
S003Learner 3A368true2026-04-02
S004Learner 4B7Nonetrue2026-04-02
S005Learner 5A472false2026-04-03
View all 18 rows
df · complete input
submission_idstudentgrouphoursscoreattendeddate
S001Learner 1A255false2026-04-01
S002Learner 2B578true2026-04-01
S003Learner 3A368true2026-04-02
S004Learner 4B7Nonetrue2026-04-02
S005Learner 5A472false2026-04-03
S006Learner 6B685true2026-04-03
S007Learner 7A8Nonetrue2026-04-04
S008Learner 8B149true2026-04-04
S009Learner 9A256false2026-04-05
S010Learner 10B579true2026-04-05
S011Learner 11A369true2026-04-06
S012Learner 12B791true2026-04-06
S013Learner 13A473false2026-04-07
S014Learner 14B686true2026-04-07
S015Learner 15A895true2026-04-08
S016Learner 16B150true2026-04-08
S017Learner 17A257false2026-04-09
S018Learner 18B580true2026-04-09

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

Your task · Compare groups fairly

Compare study groups using attendees with known scores only.

eligibleDataFramecountsSeriesmeansSeries

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.

Words in this task
eligible
rows that meet this task’s stated conditions
  1. Attendees with scoresOutputeligibleDataFrame
    1. Filter df to rows where attended is True and score is present.
    2. Store them in eligible, with every original column and row order preserved.
  2. Group countsOutputcountsSeries
    1. Store the number of rows in eligible for each group in counts.
  3. Mean scoresOutputmeansSeries
    1. Store the mean score of rows in eligible for each group in means.

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

Are the counts and averages describing the same learners?

Hint 2 — Tools

notna(), groupby(), count() and mean()

Revisit: Combine conditions · Summarise groups · Answer one numerical question

Hint 3 — Approach

Use one eligible population for both group sizes and mean scores.

Explained solution

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

eligible = df[df.attended & df.score.notna()]
counts = eligible.groupby('group')['score'].count()
means = eligible.groupby('group')['score'].mean()
means
View supplied setup

This code is already at the top of the editor.

import pandas as pd

# Supplied input: df (18 rows)
df = pd.DataFrame(
    [
        ('S001', 'Learner 1', 'A', 2, 55, False, '2026-04-01'),
        ('S002', 'Learner 2', 'B', 5, 78, True, '2026-04-01'),
        ('S003', 'Learner 3', 'A', 3, 68, True, '2026-04-02'),
        ('S004', 'Learner 4', 'B', 7, None, True, '2026-04-02'),
        ('S005', 'Learner 5', 'A', 4, 72, False, '2026-04-03'),
        ('S006', 'Learner 6', 'B', 6, 85, True, '2026-04-03'),
        ('S007', 'Learner 7', 'A', 8, None, True, '2026-04-04'),
        ('S008', 'Learner 8', 'B', 1, 49, True, '2026-04-04'),
        ('S009', 'Learner 9', 'A', 2, 56, False, '2026-04-05'),
        ('S010', 'Learner 10', 'B', 5, 79, True, '2026-04-05'),
        ('S011', 'Learner 11', 'A', 3, 69, True, '2026-04-06'),
        ('S012', 'Learner 12', 'B', 7, 91, True, '2026-04-06'),
        ('S013', 'Learner 13', 'A', 4, 73, False, '2026-04-07'),
        ('S014', 'Learner 14', 'B', 6, 86, True, '2026-04-07'),
        ('S015', 'Learner 15', 'A', 8, 95, True, '2026-04-08'),
        ('S016', 'Learner 16', 'B', 1, 50, True, '2026-04-08'),
        ('S017', 'Learner 17', 'A', 2, 57, False, '2026-04-09'),
        ('S018', 'Learner 18', 'B', 5, 80, True, '2026-04-09'),
    ],
    columns=['submission_id', 'student', 'group', 'hours', 'score', 'attended', 'date'],
)

Why these steps exist

  • One eligibility mask gives both summaries the same population. Otherwise a larger count could include unscored learners.
  • The mean excludes missing scores; a zero-filled score would change the question being answered.

Another valid approach

Named group aggregations can calculate both outputs together; expose the requested separate named results afterwards.

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.