Skip to content
Activities · Activity 3.5

Q.Write the python statements to print average marks in Science by all the students in each UT.
[Table: The chapter's case study (Program 3.1) stores unit test marks of 4 students (maximum marks 25 in each subject) in a DataFrame df created from the dictionary 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]}.]

Uttar Pradesh UpmspTextbookSubjective· 2mImportance★★★★★est
21% · 11/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 →

Group the case-study DataFrame df by UT, then take the mean of the Science column within each group: df.groupby('UT')['Science'].mean() gives UT1 = 19.75, UT2 = 19.50, UT3 = 20.00.

This activity comes directly after the chapter introduces GROUP BY (section 3.5), which the book itself frames as a split-apply-combine process:

  1. Split -- break the DataFrame into groups based on some column's values.
  2. Apply -- run an aggregate function (mean, sum, max, ...) on each group separately.
  3. Combine -- assemble the per-group results back into a single result (a Series or DataFrame).

Here, "average marks in Science by all the students in each UT" means we want to split by UT (not by Name), because we want one number per Unit Test, pooling all four students together.

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)

avg_science_by_ut = df.groupby('UT')['Science'].mean()
print(avg_science_by_ut)

Output:

UT
1    19.75
2    19.50
3    20.00
Name: Science, dtype: float64

The bigger picture -- this is one column of a larger aggregate. The book's own worked GROUP BY example (right before this activity) computes the mean of every subject at once with df.groupby(['UT']).aggregate('mean'):

UTMathsScienceS.StHindiEng
120.0019.7521.2520.2521.00
221.5019.5021.7522.0020.75
316.2520.0018.2523.2519.75

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.