Skip to content
Think & Reflect · Q1

Q.Can you write a shortened code to get the output of Program 3.4?
[Context: Program 3.4 — Write the python statement to print the total marks secured by raman in each subject. The textbook's code is:
dfRaman=df[df['Name']=='Raman']
print("Marks obtained by Raman in each test are:
", dfRaman)
dfRaman[['Maths','Science','S.St','Hindi','Eng']].sum()]

Puducherry CbseNCERTSubjective· 3mImportance★★★★★
68% · 36/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 →

Program 3.4's real output -- Raman's total marks in each subject -- can be reproduced in one chained statement: df[df['Name'] == 'Raman'][['Maths', 'Science', 'S.St', 'Hindi', 'Eng']].sum(), giving Maths 57, Science 60, S.St 50, Hindi 66, Eng 68.

Program 3.4 in the textbook is written as three separate statements:

dfRaman = df[df['Name'] == 'Raman']
print("Marks obtained by Raman in each test are:\n", dfRaman)
dfRaman[['Maths', 'Science', 'S.St', 'Hindi', 'Eng']].sum()

Using the chapter's own case-study data (marksUT from Program 3-1):

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)

...the full three-statement version produces:

dfRaman = df[df['Name'] == 'Raman']
print("Marks obtained by Raman in each test are:\n", dfRaman)

Output:

Marks obtained by Raman in each test are:
    Name  UT  Maths  Science  S.St  Hindi  Eng
0  Raman   1     22       21    18     20   21
1  Raman   2     21       20    17     22   24
2  Raman   3     14       19    15     24   23
dfRaman[['Maths', 'Science', 'S.St', 'Hindi', 'Eng']].sum()

Output:

Maths      57
Science    60
S.St       50
Hindi      66
Eng        68
dtype: int64

What "shortened" really means here. Program 3.4's purpose is the final Series of subject totals -- the intermediate print(dfRaman) line only exists to let you see Raman's three raw rows before they're summed; it is not part of the actual answer. So the "output of Program 3.4" a shortened version needs to reproduce is that final totals Series, and the filter + column-selection + .sum() can all be chained into a single expression without ever creating the named variable dfRaman:

print(df[df['Name'] == 'Raman'][['Maths', 'Science', 'S.St', 'Hindi', 'Eng']].sum())

Output:

Maths      57
Science    60
S.St       50
Hindi      66
Eng        68
dtype: int64

Key lines explained:

  • df[df['Name'] == 'Raman'] -- filters df down to Raman's three rows (index 0, 1, 2), using the same Boolean-mask mechanism as the original code's dfRaman = df[df['Name'] == 'Raman']. …

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.