All problems

Average GPA by Major

easyPythonGroupBy

The registrar at Fairhaven University publishes a one-page summary each year showing how students in each major are doing on average. This year the page carries an honours panel too: the same figure per major, but counted over the students who reached that year's honours threshold and nobody else. The faculty sets the threshold afresh each year, so it arrives with the request, and you've been handed the student table and asked for the numbers behind the panel.

The majors are wildly uneven — one has a single student on the books — so these figures are going to be fragile, and the registrar knows it. The page wants one number per major regardless.

students_df — one row per student. gpa is on the usual 0–4 scale.

id name major gpa
1 Nora Fischer Computer Science 3.8
2 Omar Haddad Statistics 3.4
3 Petra Novakova Computer Science 3.9
4 Quinn Walsh Mathematics 3.1
5 Ravi Shah Statistics 3.6

Input

The DataFrame above is already built for you — the student rows are not read from input. What does arrive is a single line holding this year's threshold, a number on the same 0–4 scale.

Task: Print a Python dict whose keys are major names and whose values are the average GPA in that major counting only the students at or above the threshold, rounded to 2 decimals. A student sitting exactly on the threshold counts. A major with nobody at or above it drops out of the dict, and if no student anywhere reaches it, print an empty dict.

Example: if one major's qualifying students averaged three point four, its entry would read 'History': 3.4.

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…