Skip to main contentSkip to solution

NameUnit TestMathsScienceHindiEnglish
Riya127262425
Riya226252624
Riya319242823
Arun128232522
Arun229262424
Arun317282319
Rohit120262426
Rohit223252625
Rohit322222723

The above DataFrame is created as 'df'. To print the total marks scored by Arun in all subjects in each unit test, we can use:

Solution

✅ Correct Option: 1

df[df['Name'] == 'Arun'] keeps only Arun's three rows (one per unit test). Selecting the four subject columns with a list, [['Maths', 'Science', 'Hindi', 'English']], and applying .sum(axis = 1) adds across the columns of each row. This gives one total per unit test:

Unit TestTotal
128+23+25+22=9828 + 23 + 25 + 22 = 98
229+26+24+24=10329 + 26 + 24 + 24 = 103
317+28+23+19=8717 + 28 + 23 + 19 = 87

Option 2: 'Name' == 'Arun' compares two plain strings and gives False, so df[False] is not a row filter and raises an error.

Option 3: dfArun['Maths', 'Science', ...] passes a tuple, not a list, so pandas raises a KeyError. Also, axis = 0 sums down each column.

Option 4: The selection is correct, but axis = 0 gives the total of each subject over the three tests, not the total of each test.

In pandas, axis = 1 works across columns (row-wise totals), and axis = 0 works down rows (column-wise totals).

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question