Skip to content
Programs · Program 3-10

Q.Write the python statements to print the mean, variance, standard deviation and quartile of the marks scored in Mathematics by each student across the UTs.

Tripura TbseTextbookSubjective· 3mImportance★★★★★est
32% · 17/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 →

groupby() splits df into one group per student, following a split → apply → combine pattern; ['Maths'] then picks that column within each group, and .agg([...]) applies several statistics to it at once, producing one summary row per student.

The split-apply-combine idea

groupby() works in three conceptual steps: split the DataFrame into groups sharing some key (here, Name); apply a function to each group separately; combine the per-group results back into one summary table. A small illustration: if a two-column table held keys A/B/C paired with several numbers each, groupby('key').sum() would split it into three little tables (one per key), sum each one independently, then stitch the three sums back into a single 3-row result — that's exactly the same mechanism used here, just with Name as the key and four richer statistics instead of one sum.

What a GroupBy object actually holds, before aggregating

It helps to see what df.groupby('Name') produces before jumping straight to .agg():

g1 = df.groupby('Name')
print(g1.first())      # the first row of each student's group
print(g1.size())        # how many rows are in each group
print(g1.get_group('Raman'))   # pull out one student's full group
         UT  Maths  Science  S.St  Hindi  Eng
Name
Ashravy   1     23       19    20     15   22
Mishti    1     15       22    25     22   22
Raman     1     22       21    18     20   21
Zuhaire   1     20       17    22     24   19

Name
Ashravy    3
Mishti     3
Raman      3
Zuhaire    3
dtype: int64

   UT  Maths  Science  S.St  Hindi  Eng
0   1     22       21    18     20   21
1   2     21       20    17     22   24
2   3     14       19    15     24   23

Each student's group is genuinely just their 3 original rows, held together under one label — .agg() is what turns each of those 3-row groups into a single summary row.

Program 3-10 — four statistics in one call

print(df.groupby(by='Name')['Maths'].agg(['mean', 'var', 'std', 'quantile']))

Output:

              mean       var        std       quantile
Ashravy  19.666667  44.333333  6.658328   23.0
Mishti   16.666667   2.333333  1.527525   17.0
Raman    19.000000  19.000000  4.358899   21.0
Zuhaire  21.666667   2.333333  1.527525   22.0

Interpreting what the four numbers mean …

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.