In this tutorial, we will look at how to rename the categories in a Pandas category type column (or series) with the help of examples.
How to rename categories in Pandas?

You can use the Pandas rename_categories()
function to rename the categories in a category type column in Pandas. The following is the syntax –
# rename categories df["Col"] = df["Col"].cat.rename_categories(list_of_new_categories)
Pass the new categories list as an argument to the function. You can also pass a dictionary mapping of old category names to new category names.
Examples
Let’s look at some examples of renaming categories for a category type field in Pandas. First, let’s create a Pandas dataframe with a category
type column.
import pandas as pd # create a dataframe df = pd.DataFrame({ "Name": ["Tim", "Sarah", "Hasan", "Jyoti", "Jack"], "Gender": ["M", "F", "M", "F", "M"] }) # change to category dtype df["Gender"] = df["Gender"].astype("category") # display the "Gender" column print(df["Gender"])
Output:
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
0 M 1 F 2 M 3 F 4 M Name: Gender, dtype: category Categories (2, object): ['F', 'M']
We now have a dataframe containing information on names and their respective genders. Note that the “Gender” column is of category type.
Let’s now rename the categories in the “Gender” column, “F” to “Female” and “M” to “Male” using the rename_categories()
function.
# rename categories df["Gender"] = df["Gender"].cat.rename_categories(["Female", "Male"]) # display the "Gender" column print(df["Gender"])
Output:
0 Male 1 Female 2 Male 3 Female 4 Male Name: Gender, dtype: category Categories (2, object): ['Female', 'Male']
You can see that the category values in the above series have been updated. Note that it’s important to pass new category names corresponding to the order that is present in the original category field. For example, the categories in the original field are shown as ["F", "M"]
and thus we pass the new category names having the same relative order ["Female", "Male"]
.
You can also pass a dictionary mapping of old category names to new category names to the rename_categories()
function. Let’s now change the names of the categories back to “F” and “M” using this method.
# rename categories df["Gender"] = df["Gender"].cat.rename_categories({"Female": "F", "Male": "M"}) # display the "Gender" column print(df["Gender"])
Output:
0 M 1 F 2 M 3 F 4 M Name: Gender, dtype: category Categories (2, object): ['F', 'M']
You can see that we get the original category names, “F” for “Female” and “M” and “Male”.
Alternatively, you can also rename the categories by assigning new values to the categories
property of the category type series.
# rename categories df["Gender"].cat.categories = ["Female", "Male"] # display the "Gender" column print(df["Gender"])
Output:
0 Male 1 Female 2 Male 3 Female 4 Male Name: Gender, dtype: category Categories (2, object): ['Female', 'Male']
You can see that we get the same result that we got with the rename_categories()
function.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.