Skip to content
Activities · Activity 3.6

Q.Consider the data of unit test marks given at program 3.1, write the python statements to print name wise UT marks in mathematics.
[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]}.]

Uttarakhand UbseTextbookSubjective· 2mImportance★★★★★est
25% · 13/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 →

This activity sits immediately after the chapter's pivot() lesson (Example 3.1, reshaping the Store/Year sales data), so the technique it is really asking you to practise is df.pivot(index='Name', columns='UT', values='Maths') -- it reshapes the 12-row "long" case-study data into a 4-row-by-3-column grid of Maths marks, one row per student, one column per Unit Test.

The case-study DataFrame df stores data in long format: each row is one student's marks in one Unit Test, so a given student's Maths marks are spread across three separate rows.

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)

"Name wise UT marks in mathematics" is exactly what reshaping solves: we want the data turned around so that Name becomes the row label, UT becomes the column label, and Maths fills in the cells -- a wide format. This is the identical pattern the chapter just taught with pivot1 = df.pivot(index='Store', columns='Year', values='Total_sales(Rs)') for the Store/Year sales example; here Name plays the role Store played, and UT plays the role Year played.

name_wise_maths = df.pivot(index='Name', columns='UT', values='Maths')
print(name_wise_maths)

Output:

UT        1   2   3
Name
Ashravy  23  24  12
Mishti   15  18  17
Raman    22  21  14
Zuhaire  20  23  22

Why pivot() works cleanly here: pivot() requires that every (index, columns) pair be unique -- i.e. there must be exactly one row for each (Name, UT) combination, otherwise pandas raises a ValueError (it wouldn't know which value to place in a cell with more than one match). Since each student has exactly one row per Unit Test in this DataFrame, (Name, UT) pairs are all unique, so the pivot succeeds without needing an aggregate function.

Alternative: groupby() with apply(list). If instead of a readable grid you want each student's three Maths marks collected as a Python list (useful if you plan to process them further in code), groupby() is the tool:

name_wise_list = df.groupby('Name')['Maths'].apply(list)
print(name_wise_list)

Output:

Name
Ashravy    [23, 24, 12]
Mishti     [15, 18, 17]
Raman      [22, 21, 14]
Zuhaire    [20, 23, 22]
Name: Maths, dtype: object
``` …

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.