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:
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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
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.