Skip to content
Question

Q.Carefully observe the following code: import pandas as pd dic = {'pid' : [101, 102, 103, 104, 105], 'pname' : ['Shyam', 'Roushan', 'Archit', 'Medha', 'Lalit'], 'sports' : ['Cricket', 'Tennis', 'Football', 'Cricket', 'Cricket'], 'points' : [45000, 20000, 15000, 53000, 60000]} player = pd.DataFrame(dic) print(player) Write Python statements for the following:

(i) In the data frame player created above, set the row labels as 'Player 1', 'Player 2', 'Player 3', 'Player 4', 'Player 5'.
(ii) Rename the column 'points' to 'net point' in the Data Frame player.
CBSECBSE Class XII Board 2023Subjective· 2mImportance★★★★★
🔒 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 question tests your ability to modify a Pandas DataFrame's row labels and column names after creation — two fundamental data-wrangling operations.

Let's first understand what the given code does. It creates a dictionary dic with four keys — pid, pname, sports, and points — each mapped to a list of five values. When you pass this dictionary to pd.DataFrame(), Pandas automatically uses the dictionary keys as column names and assigns default integer row labels (0, 1, 2, 3, 4). The resulting DataFrame player looks like this:

   pid    pname    sports  points
0  101    Shyam   Cricket   45000
1  102  Roushan    Tennis   20000
2  103   Archit  Football   15000
3  104    Medha   Cricket   53000
4  105    Lalit   Cricket   60000

Now, the question asks you to write Python statements for two specific modifications.

Part (i): Setting custom row labels

The default row labels (0 through 4) are fine for many purposes, but sometimes you want more meaningful identifiers. Here, you need to replace them with 'Player 1', 'Player 2', and so on. The Pandas attribute that holds row labels is called the index. You can assign a new index using the .index attribute directly.

The statement is:

player.index = ['Player 1', 'Player 2', 'Player 3', 'Player 4', 'Player 5']

This overwrites the existing index with your custom list. After this, the DataFrame will display with those labels on the left.

Note

The length of the new index list must exactly match the number of rows in the DataFrame — here, five rows, so five labels. If the lengths differ, Pandas will raise an error.

Part (ii): Renaming the 'points' column to 'net point'

Column names are stored in the .columns attribute, but you should not directly assign to .columns for a single rename — that would require you to list every column name. Instead, Pandas provides the .rename() method, which lets you change specific column names using a dictionary mapping old names to new names.

The statement is:

player.rename(columns={'points': 'net point'}, inplace=True)
``` …

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.