Skip to content
Activities · Activity 2.8

Q.(a) Using the DataFrame ResultDF, write the statement to access Marks of Arnab in Maths.

(b) Create a DataFrame having 5 rows and write the statement to get the first 4 rows of it.
[Context: In the textbook, ResultSheet is a dictionary of five Series — 'Arnab': pd.Series([90, 91, 97], index=['Maths','Science','Hindi']), and similarly 'Ramit', 'Samridhi', 'Riya', 'Mallika' — and ResultDF = pd.DataFrame(ResultSheet), giving columns Arnab, Ramit, Samridhi, Riya, Mallika with rows Maths: 90 92 89 81 94; Science: 91 81 91 71 95; Hindi: 97 96 88 67 99.]
Uttarakhand UbseTextbookSubjective· 3mImportance★★★★★est
21% · 8/38 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 →

Part (a) uses .loc[] with row label 'Maths' and column label 'Arnab' to extract a single cell. Part (b) uses .head(4) on any DataFrame to get the first 4 rows.

(a) Accessing Marks of Arnab in Maths

The DataFrame ResultDF is built from a dictionary of Series, where each Series is a student's marks indexed by subject names. This means the row labels are the subjects ('Maths', 'Science', 'Hindi') and the column labels are the student names ('Arnab', 'Ramit', etc.).

To access a single cell, you need both the row and column label. The pandas tool for label-based access is .loc[]. You pass the row label first, then the column label, separated by a comma.

arnab_maths = ResultDF.loc['Maths', 'Arnab']

This returns the integer 90.

Watch out

A common mistake is to use .iloc[] with integer positions instead of .loc[] with labels. While .iloc[0, 0] would also give 90 here (since Maths is the first row and Arnab is the first column), this is fragile — if the order of rows or columns changes, .iloc breaks. Always use .loc when you know the actual labels.

(b) Creating a DataFrame and Getting First 4 Rows

First, create any DataFrame with at least 5 rows. A simple way is from a dictionary of lists:

import pandas as pd

data = {
    'Name': ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'],
    'Score': [85, 92, 78, 95, 88]
}
df = pd.DataFrame(data)
``` …

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.