All problems

Average Grade per Course (Ignoring Missing Grades)

mediumPythonNull HandlingAggregation

You're a data analyst at Ashgrove University. The curriculum committee reviews the catalogue one term at a time and flags any course whose average grade has slipped, so the number each course is judged on had better be defensible. The term under review arrives with the request.

One enrollment in the records has no grade at all: the student took an incomplete and will be marked next term. The committee settled the rule before seeing any numbers, precisely so nobody could argue with it afterwards — a missing grade is not a bad grade, it is no information, and a course must not be penalised for a mark that has not been entered yet.

enrollments_df — one row per student per course per semester. grade is on a 4.0 scale; one row is None, meaning no grade has been entered.

student_id course_id grade semester
1 1 3.7 Fall2023
1 2 3.9 Spring2024
2 1 3.2 Fall2023
2 3 3.5 Fall2023
3 2 4.0 Spring2024
3 4 3.8 Spring2024
4 3 2.9 Fall2023
5 1 3.6 Fall2023
5 2 None Spring2024

Input

The DataFrame above is already built for you — the enrollment rows are not read from input. What does arrive is a single line naming the semester under review, spelled exactly as the semester column spells it.

Task: Print a dict keyed by course_id as a plain integer, mapping each course taught in that semester to its average grade that semester, rounded to 2 decimals, with the keys in ascending order. An enrollment with no grade must be left out of the average entirely: it must not count as a zero, and it must not count towards how many grades were averaged. A semester with no enrollments on record has no courses to report, so print an empty dict.

Example: if course 9 averaged two and a half, you'd print {9: 2.5}.

Sign in to solve this problem

Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.

Sign in

Discussion

Sign in to join the discussion — reading is open to everyone.

Loading comments…