Question:

Which of the following Python statements is used to change a column label in a DataFrame, df ?

Show Hint

Always use a dictionary inside rename() to change column names, and specify axis properly.
Updated On: Jul 14, 2025
  • df = df.rename(old_name : new_name, axis='columns')
  • df = df.rename(old_name, new_name), axis='columns'
  • df = df.change_name(old_name, new_name, axis='bar')
  • df = df.update(old_name : new_name, axis='bar')
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is A

Solution and Explanation

In Python Pandas, the standard way to change a column name is by using the rename() method.
This method allows you to pass a dictionary that maps the old column name to the new column name.
You must also specify axis='columns' or axis=1 to indicate that the renaming is for column labels.
So the syntax looks like: df = df.rename({old_name : new_name}, axis='columns').
Option (B) is invalid because rename() does not accept positional arguments like old_name and new_name separately.
Option (C) is invalid because there is no method called change_name() in Pandas.
Option (D) is invalid because update() does not rename columns; it updates data values.
Therefore, the correct way to rename a column is option (A).
Was this answer helpful?
0
0

Top Questions on Programming in Python

View More Questions