Q.Write the statements to output the maximum marks obtained in each subject in Unit Test 2.
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Filter the case-study DataFrame df to just the Unit Test 2 rows with a Boolean mask, then call .max(numeric_only=True) — and note the one genuine "gotcha" of max()/min(): their axis argument works backwards compared to almost every other statistical method in this chapter.
Step 1 — filter to Unit Test 2
dfUT2 = df[df.UT == 2]
print('\nResult of Unit Test 2:\n\n', dfUT2)
df.UT == 2 compares every row's UT value to 2 and produces a Boolean Series (True/False, one per row). Passing that Boolean Series back into df[...] keeps only the rows where it's True — here, one row per student (the one where each student sat their second unit test):
Output:
Result of Unit Test 2:
Name UT Maths Science S.St Hindi Eng
1 Raman 2 21 20 17 22 24
4 Zuhaire 2 23 15 21 25 15
7 Ashravy 2 24 22 24 17 21
10 Mishti 2 18 21 25 24 23
Step 2 — take the column-wise maximum
print('\nMaximum Mark obtained in Each Subject in Unit Test 2:\n\n',
dfUT2.max(numeric_only=True))
Output:
Maximum Mark obtained in Each Subject in Unit Test 2:
UT 2
Maths 24
Science 22
S.St 25
Hindi 25
Eng 24
dtype: int64
Why numeric_only=True matters: without it, dfUT2.max() would also try to find a "maximum" for the Name column — and since text columns are compared alphabetically, that would silently add a Name Zuhaire line to the result (Zuhaire sorts last alphabetically among Raman/Zuhaire/Ashravy/Mishti). numeric_only=True restricts the calculation to columns that are actually numbers, so only UT, Maths, Science, S.St, Hindi and Eng come back.
The one-liner shortcut
The two steps above can be combined into a single statement:
dfUT2 = df[df['UT'] == 2].max(numeric_only=True)
print(dfUT2)
This filters and reduces in one line, skipping the intermediate print of the 4-row table — functionally identical output, just without the "show your working" step.
The axis gotcha, worth knowing …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.