Rename column names in Pandas

Pandas is a powerful and flexible data manipulation library in Python that provides a variety of features for working with tabular data. If you are working with Pandas DataFrames which is a 2-dimensional data structure like an array or table with rows and columns, then you might want to know how to rename a column name in Pandas. Let’s understand the methods of renaming a column name with an example.

How to rename a column name in Pandas?

There are some most common methods by which you can simply rename a column name in pandas.

Method 1- Rename a column name using rename() function

A column name can be renamed by using the rename() function in pandas DataFrames. This method is useful when you want to rename a specific column. You just need to provide the information of the column you want to rename.

To rename the columns ‘name’ and ‘id’ we wil use rename() function. The syntax of the function is given below.

rename(columns={"OLD_COLUMN_VALUE": "NEW_COLUMN_VALUE"}) 

e.g. – In the following example we have created a Python dictionary that consists of two columns ‘name’ and ‘id’ of a patient, and converted it to a DataFrame.

import pandas as pd
patient = {
    "name": ["giggle", "Jahe", "Jane", "nil"], 
    "id": ["45", "46", "47", "48"]
}
# convert patient names into a Dataframe
df = pd.DataFrame(patient)
# renaming the columns name 
df.rename(columns={"name": "Fullname", "id": "ID"}, inplace=True)
#Print the DataFrame after renaming
print(df)

Output :

The “inplace=True” parameter indicates that the operation will modify the object in place, and you don’t need to assign the result back to a variable.

Method 2- Rename a column name using a list

Another way to rename columns is to directly assign a list of new column names to the columns attribute of the DataFrame. This is also the simplest and straightforward method to rename all the column names in one go.

e.g. – Here we are assigning a list of new column names to rename all the columns at a time.

# rename the column by assigning the new column names list
df.columns = ["FULLNAME", "ID"]
# After renaming the columns
print(df)

Output :

Method 3- Rename a column name using the set_axis() Function

Here we will use the set_axis() function to rename a column name by specifying the new labels for the columns. Keep in mind that set_axis() is versatile and can be used to rename both columns and row labels by changing the axis parameter accordingly.
This function has the following syntax.

set_axis([NEW_COLUMN_NAME,...], axis="columns")
#rename the column
df.set_axis(["FULLNAME", "ID"], axis="columns", inplace=True) 
# After renaming the columns
print(df)

Output :

Method 4- Rename using add_prefix() and add_suffix() functions

If you want to add a prefix or suffix to your column name, you can use the add_prefix() and add_suffix() functions to add a prefix or suffix to the existing column names. This method will not allow you to replace the entire column name as the rename() method does.

e.g.- You have to use a for loop to assign a prefix or suffix to the each column name. Here we are adding prefix to the columns ‘name’ and ‘id’.

# Rename the column by adding a prefix
new_prefix = 'Prefix_'
df.columns = [new_prefix + col for col in df.columns]
# After renaming the columns
print(df)

Output :

Method 5- Rename using str.replace() function

You can use the str.replace() function in Pandas to rename a column by replacing specific parts of the column name with a new string. The str.replace() function takes two parameters, first one is old column name and second one is the new column name. Here is an example how you can achieve this.

#replace a column 'name' of patient
df.columns = df.columns.str.replace('name', 'FULLNAME') 

All of the above method will help you to rename a column in Pandas and give the same result after renaming the columns. You can use any of them that is convenient to you.