Which of the following method can be used to rename categorical data?

Categorical.rename_categories()
Categorical.rename()
Categorical.mv_categories()
None of the mentioned

The correct answer is: A. Categorical.rename_categories()

The Categorical.rename_categories() method can be used to rename categorical data. It takes two arguments: the old name of the category and the new name of the category. For example, to rename the category “Male” to “Man”, you would use the following code:

df['gender'] = df['gender'].rename_categories({'Male': 'Man'})

The Categorical.rename() method can also be used to rename categorical data, but it is less flexible than the Categorical.rename_categories() method. The Categorical.rename() method takes only one argument: the new name of the category. For example, to rename the category “Male” to “Man”, you would use the following code:

df['gender'] = df['gender'].rename('Man')

The Categorical.mv_categories() method is not used to rename categorical data. It is used to move categories around in a categorical column. For example, to move the category “Male” to the beginning of the categorical column, you would use the following code:

df['gender'] = df['gender'].mv_categories('Male', 0)

Exit mobile version