Q.Try to write a short code to get the above output. Remember to print the relevant headings of the output.
[Context: The referenced textbook output is the average of marks obtained by Zuhaire in all Unit Tests, produced by:
dfZuhaireMarks = dfZuhaire.loc[:,'Maths':'Eng']
print("Average of marks obtained by Zuhaire in all Unit Tests
", dfZuhaireMarks.mean(axis=1))
giving:
3 20.4
4 19.8
5 19.0
dtype: float64]
dfZuhaireMarks = dfZuhaire.loc[:,'Maths':'Eng']
print("Average of marks obtained by Zuhaire in all Unit Tests
", dfZuhaireMarks.mean(axis=1))
giving:
3 20.4
4 19.8
5 19.0
dtype: float64]
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 to Zuhaire's rows, slice out just the subject-mark columns ('Maths' through 'Eng'), and take the row-wise mean with axis=1. Built from the real case-study data end to end, this reproduces the book's own output exactly: 20.4 (UT1), 19.8 (UT2), 19.0 (UT3).
This activity immediately follows Program 3-6, which computes the same statistic for Zuhaire using a dfZuhaire that is assumed already filtered. To write a genuinely short, self-contained version, we build it starting from the full case-study DataFrame so every step is traceable back to real data:
import pandas as pd
marksUT = {
'Name': ['Raman', 'Raman', 'Raman', 'Zuhaire', 'Zuhaire', 'Zuhaire',
'Ashravy', 'Ashravy', 'Ashravy', 'Mishti', 'Mishti', 'Mishti'],
'UT': [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3],
'Maths': [22, 21, 14, 20, 23, 22, 23, 24, 12, 15, 18, 17],
'Science': [21, 20, 19, 17, 15, 18, 19, 22, 25, 22, 21, 18],
'S.St': [18, 17, 15, 22, 21, 19, 20, 24, 19, 25, 25, 20],
'Hindi': [20, 22, 24, 24, 25, 23, 15, 17, 21, 22, 24, 25],
'Eng': [21, 24, 23, 19, 15, 13, 22, 21, 23, 22, 23, 20]
}
df = pd.DataFrame(marksUT)
# Filter to Zuhaire's three rows (index 3, 4, 5)
dfZuhaire = df[df['Name'] == 'Zuhaire']
# Print heading + row-wise average of the subject-mark columns
print("Average of marks obtained by Zuhaire in all Unit Tests\n",
dfZuhaire.loc[:, 'Maths':'Eng'].mean(axis=1))
Output:
Average of marks obtained by Zuhaire in all Unit Tests
3 20.4
4 19.8
5 19.0
dtype: float64
This matches the book's printed result exactly, and each value can be verified by hand from Zuhaire's real marks:
- UT1 (index 3): Maths 20, Science 17, S.St 22, Hindi 24, Eng 19 →
- UT2 (index 4): Maths 23, Science 15, S.St 21, Hindi 25, Eng 15 →
- UT3 (index 5): Maths 22, Science 18, S.St 19, Hindi 23, Eng 13 →
Key lines explained:
df[df['Name'] == 'Zuhaire']-- the same Boolean-mask filter used throughout the chapter; it returns Zuhaire's three original rows, keeping their original index labels (3, 4, 5) rather than renumbering from 0..loc[:, 'Maths':'Eng']--.locwith a column label slice. Because the DataFrame's columns are in the fixed orderName, UT, Maths, Science, S.St, Hindi, Eng, slicing from'Maths'to'Eng'grabs every column in between inclusively -- all five subject-mark columns -- without having to type out each name, and without includingName/UT. …
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.