Question 51 of 53
Q.Which of the following Python statements is used to change a column label in a DataFrame, df? (A) df = df.rename({old_name: new_name}, axis='columns') (B) df = df.rename(old_name, new_name), axis='columns' (C) df = df.change_name(old_name, new_name, axis='bar') (D) df = df.update({old_name: new_name}, axis='bar')
CBSECBSE Class XII Board 2025MCQ· 1mImportance★★★★★
96% · 51/53 Questions
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 →Option (A) is correct: df = df.rename({old_name: new_name}, axis='columns'). The pandas rename() method takes a dictionary mapping old labels to new labels, and axis='columns' targets column labels. The other options use invalid syntax, a non-existent method, or the wrong method.
Concept — renaming a column label in pandas
A DataFrame's column names live in its column index. To change one, pandas provides the rename() method, which accepts a dictionary mapping ({old: new}) and an axis argument ('columns' or 1 for column labels). rename() returns a new DataFrame, so the result must be reassigned back to df.
import pandas as pd
df = pd.DataFrame({'old_name': [1, 2, 3]})
df = df.rename({'old_name': 'new_name'}, axis='columns')
print(df)
Expected output:
new_name
0 1
1 2
2 3
Why each other option is wrong
- (B)
df = df.rename(old_name, new_name), axis='columns'—rename()does not take old and new names as two positional arguments, and the stray, axis='columns'sits outside the call's parentheses, which is a syntax error (it would build a tuple, not pass a keyword argument). …
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.