Skip to content
Programs · Program 3-6

Q.Write the statements to get an average of marks obtained by Zuhaire in all the Unit Tests.

Tripura TbseTextbookSubjective· 2mImportance★★★★★
23% · 12/53 Questions
🔒 Locked · start free trial →

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 to Zuhaire's rows, slice out just the subject columns with label-based .loc[:, 'Maths':'Eng'], then take .mean(axis=1) to average ACROSS the subjects for each unit test — one average per row.

Step 1 — filter to Zuhaire

dfZuhaire = df[df.Name == 'Zuhaire']

This keeps Zuhaire's 3 rows (index 3, 4 and 5 — his UT1, UT2 and UT3).

Step 2 — slice to just the subject columns

dfZuhaireMarks = dfZuhaire.loc[:, 'Maths':'Eng']
print("Slicing of the DataFrame to get only the marks\n", dfZuhaireMarks)

Output:

Slicing of the DataFrame to get only the marks
    Maths  Science  S.St  Hindi  Eng
3     20       17    22     24   19
4     23       15    21     25   15
5     22       18    19     23   13

Why .loc[:, 'Maths':'Eng'] instead of a column list: this is label-based slicing, not position-based. Maths and Eng are the first and last of the five subject columns in df's column order, so 'Maths':'Eng' grabs everything in between — dropping Name and UT, which sit before Maths. One quirk to remember: unlike ordinary Python slicing, .loc label slices are inclusive of BOTH endpoints — 'Eng' itself is included in the result, not excluded the way list[a:b] would exclude b.

Step 3 — average across subjects, per test

print("Average of marks obtained by Zuhaire in all Unit Tests\n",
      dfZuhaireMarks.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
``` …

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.