Name Unit Test Maths Science Hindi English Riya 1 27 26 24 25 Riya 2 26 25 26 24 Riya 3 19 24 28 23 Arun 1 28 23 25 22 Arun 2 29 26 24 24 Arun 3 17 28 23 19 Rohit 1 20 26 24 26 Rohit 2 23 25 26 25 Rohit 3 22 22 27 23
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:
| Name | Unit Test | Maths | Science | Hindi | English |
|---|---|---|---|---|---|
| Riya | 1 | 27 | 26 | 24 | 25 |
| Riya | 2 | 26 | 25 | 26 | 24 |
| Riya | 3 | 19 | 24 | 28 | 23 |
| Arun | 1 | 28 | 23 | 25 | 22 |
| Arun | 2 | 29 | 26 | 24 | 24 |
| Arun | 3 | 17 | 28 | 23 | 19 |
| Rohit | 1 | 20 | 26 | 24 | 26 |
| Rohit | 2 | 23 | 25 | 26 | 25 |
| Rohit | 3 | 22 | 22 | 27 | 23 |
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
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 Test | Total |
|---|---|
| 1 | |
| 2 | |
| 3 |
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).
Related questions:
2026: 21 May Shift 1
2026: 23 May Shift 1
2025: 3 June Shift 2