Home

 › 

Articles

 › 

How to Rename Columns in Pandas DataFrame

python language

How to Rename Columns in Pandas DataFrame

When it comes to renaming columns in Pandas DataFrame, there are a few options available to you. Renaming columns can be as simple as using the rename function to change a single column, but it can also be more elaborate. You may need to rename a column if the label isn’t descriptive enough, has unwanted character such as spaces, or the column completely lacks a name altogether. In these cases, you’ll find your answers in this article. Read on to discover all the methods to rename columns in Pandas DataFrame.

How to Rename a Single Column Using the Rename Function

If you want to rename a single column, the process is pretty simple. Check it out below.

Step 1: Import Pandas

To start, you’ll need to import Pandas into whichever environment you’re using. For these illustrations, we’re using Spyder.

import pandas as pd
Rename columns in Pandas
Start by importing Pandas into whichever environment you’re using.

Step 2: Define the Dictionary

Before we have data to work with, we need to create it. To do this, you need to input the desired name of your DataFrame, followed by the first column and its values, then the other columns. This is shown in the screenshot below. Doing this will create a dictionary, which is a data structure. Here’s the code that was used for this:

Data = {'Name': ['Samantha', 'Daniel', 'Louise', 'Patrick', 'Richard'],
'Height': ['182', '196', '154', '177', '194'],
'Weight': ['166', '210', '134', '168', '212'],
'Age': ['28', '39', '41', '26', '32']}
Rename columns in Pandas
First you’ll need to create a dictionary, which is a data structure.

Step 3: Convert to a DataFrame

Now, we need to convert this data to a DataFrame. This can be done with the code

df = pd.DataFrame(Data)

replacing “Data” with whatever name you chose. By inputting “df” next, you can see a representation of your DataFrame to confirm, as is illustrated.

Rename columns in Pandas
Convert the data to a DataFrame by typing in the code as shown above.

Step 4: Rename the Column

Finally, we can rename a single column by using this code:

df.rename(columns={'Name':'NAME'})

where you can replace “Name” with the column label you want to change, and “NAME” with the desired label.

Rename columns in Pandas
Replace “Name” with the column label you want to change, and replace “NAME” with your new label.

Step 5: Rename Multiple Columns Using the Rename Function

A similar method can be used to rename multiple columns. Simply adjust the code to include the other column labels like this:

df.rename(columns={'Height':'HEIGHT', 'Weight':'WEIGHT', 'Age':'AGE'})

After doing this, the DataFrame will be returned to you automatically.

Rename columns in Pandas
Rename multiple columns by changing the code to include the other column labels.

Step 6: Rename Columns by Assigning New Column Names

This is another way to rename your columns. The drawback of this method is that you need to list all of the column names, including the ones you don’t want to change. This can be achieved using this code:

df.columns= ['NAME', 'HEIGHT', 'WEIGHT', 'AGE']
Rename columns in Pandas
Using this method, you need to list all of the column names, even those you don’t want to change.

Step 7: Rename Columns Using the Set_axis Function

An alternative is to assign the desired labels, or index, using the “set_axis” function. This can be used to assign names to the desired axis. In this case, the desired axis is the columns, and the code is inputted like this:

df.set_axis{['A', 'B', 'C', 'D'], axis='columns')
Rename columns in Pandas
The “set_axis” function can be used to assign names to the desired axis.

Step 8: Rename Columns by Adding a Prefix and Suffix

Yet another method for renaming columns is to add a prefix and suffix to their labels. This can be useful if you want to elaborate on the column names without replacing them completely. Do this with the following code:

df = df.add_prefix('1_')

df = df.add_suffix('_1')
Rename columns in Pandas
Rename columns by adding a prefix and suffix to their labels, especially if you do not want to replace them.

Step 9: Rename Columns by Replacing Specific Text

Technically, this method uses the replace function rather than the rename function, but it works in a similar way. It’s particularly helpful for replacing specific parts of the column label. You can achieve this with this code:

df.columns=df.columns.str.replace('1_NAME_1', 'NAME')

Bear in mind that you’ll need to input a line of code for each column label you want to replace.

Rename columns in Pandas
Use the replace function instead of the rename function to replace specific parts of the column label.

In the following video, Analytics4All provides an excellent demonstration of how to rename a column in Pandas DataFrame in Python.

Wrapping Up

We’ve covered several methods for renaming single or multiple columns. The simplest is probably using the rename function or assigning new names, but the latter requires you to type out every column name in the DataFrame. This may be inconvenient if you’re dealing with especially large amounts of data. Another method is to assign labels to the column axis with the set_axis function. You can also add a prefix or suffix with the add_prefix and add_suffix functions respectively, or you can replace specific parts of the column label using the str.replace function. Overall, there are a variety of methods for how to rename columns in Pandas, each with their own functions and use cases.

Up Next…

Frequently Asked Questions

Can a column be renamed in Pandas?

Yes, there are several functions you can use to rename your columns in Pandas DataFrame.

How do I rename a column in Pandas?

The simplest way is to use the rename function, but you can also use set_axis, add_prefix, add_suffix or str.replace.

Why would you want to rename a column?

You may need to rename a column that has a label which isn’t intuitive, descriptive, or appropriate for the data it describes. The column name could also contain some unwanted characters, or it may be completely absent altogether.

How do I rename multiple columns in Pandas?

You can use the rename function to rename multiple columns, as well as the set_axis, add_prefix, add_suffix or str.replace functions, depending on how many you need to rename and which parts of the text.

What's the difference between rename and replace?

Rename is only used for renaming columns, but replace can also be used to replace values with another desired value throughout the DataFrame. Replace also supports Regex.

To top