While working with data you may require to change the names of some or all the columns of a dataframe. In this tutorial, we’ll cover three different ways in Pandas to rename column names along with examples.
How to Rename a Column in Pandas
You can use the Pandas dataframe rename()
function to rename column names in Pandas. There are other methods for renaming columns as well. The following are some easy, useful, and effective ways to change the name of columns in Pandas –
Table of Contents
How to Rename a Column in Pandas
Top 3 Methods to Rename Column Names in Panda
- Method 1: Use Pandas dataframe rename() function to rename columns
- Method 2: Use Pandas dataframe set_axis() method to rename column names
- Method 3: Rename columns in Pandas dataframe by changing its attribute
Summary: Changing Column Names in Pandas (Key Takeaways)
Top 3 Methods to Rename Column Names in Pandas
- Method 1: Use the Pandas dataframe
rename()
function to modify specific column names. - Method 2: Use the Pandas dataframe
set_axis()
method to change all your column names. - Method 3: Set the dataframe’s
columns
attribute to your new list of column names.
Let’s explore in more detail by examing practical examples of using Pandas rename column names methods to manipulate and manage columns for data.
Method 1: Use Pandas rename()
function to rename columns
The Pandas dataframe rename()
function is a quite versatile function used not only to rename column names but also row indices.
You can use this function to rename specific columns. Note that we can also rename a single column or multiple columns. The following is the syntax to change column names using the Pandas rename()
function.
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.
df.rename(columns={"OldName":"NewName"})
The rename() function returns a new dataframe with renamed axis labels (i.e. the renamed columns or rows depending on usage). To modify the dataframe in place set the argument inplace
to True
.
Let’s now look at some examples.
Example: Change the name of a specific column
Here, we will create a dataframe storing the category and color information of some pets in the columns “Category” and “Color” respectively.
import pandas as pd # data of pets data = {'Category': ['Dog', 'Cat', 'Rabbit', 'Parrot'], 'Color': ['brown', 'black', 'white', 'green']} # create pandas dataframe df = pd.DataFrame(data) # display the dataframe df
Output:
We now have a dataframe containing the “Category” and “Color” information of some pets.
Let’s now use the rename()
function to change the name of the “Category” column to “Pet”.
# change column name "Category" to "Pet" df = df.rename(columns={"Category":"Pet"}) # display the dataframe df
Output:
You can see that the column name has been changed.
Note that you can also just get the column names of a Pandas dataframe using df.columns
.
Example: Apply rename()
function to column names
The rename()
function also accepts a function that can be applied to each column name. For example, you can write a function to replace special characters like underscore _ with a space, split the column name on a delimiter, keep only a specific portion, etc.
import pandas as pd # create a dataframe data = {'Col1_Category': ['Dog', 'Cat', 'Rabbit', 'Parrot'], 'Col2_Color': ['brown', 'black', 'white', 'green']} df = pd.DataFrame(data) # print dataframe columns print("Dataframe columns:", df.columns) # change column names to the string after the _ df = df.rename(columns=lambda x: x.split("_")[1]) # print dataframe columns print("Dataframe columns:", df.columns)
Output:
Dataframe columns: Index(['Col1_Category', 'Col2_Color'], dtype='object') Dataframe columns: Index(['Category', 'Color'], dtype='object')
In the above example, we pass a function to the rename function to modify the column names. The function gets applied to each column and gives its respective new name. Here, we split each column name on _
and use the second string as our new name for that column.
Method 2: Use Pandas set_axis()
function to rename column names
The Pandas dataframe set_axis() method can be used to rename a dataframe’s columns by passing a list of all columns with their new names.
Note that the length of this list must be equal to the number of columns in the dataframe. The following is the syntax:
df.set_axis(new_column_list, axis=1)
You have to explicitly specify the axis as 1
or 'columns'
to update column names since its default is 0
(which modifies the axis for rows).
It returns a new dataframe with the updated axis. To modify the dataframe in place, set the argument inplace
to True
.
Let’s now look at an example.
Example: Change the name of a column using set_axis()
We’ll take the same use case as above, and change the column name “Category” to “Pet” in a dataframe but this time we will be using the set_axis()
method.
import pandas as pd # data of pets data = {'Category': ['Dog', 'Cat', 'Rabbit', 'Parrot'], 'Color': ['brown', 'black', 'white', 'green']} # create pandas dataframe df = pd.DataFrame(data) # print dataframe columns print("Dataframe columns:", df.columns) # change column name Category to Pet df = df.set_axis(["Pet", "Color"], axis=1) # print dataframe columns print("Dataframe columns:", df.columns)
Output:
Dataframe columns: Index(['Category', 'Color'], dtype='object') Dataframe columns: Index(['Pet', 'Color'], dtype='object')
In the above example, the set_axis()
function is used to rename the column Category
to Pet
in the dataframe df
. Note that we had to provide the list of all the columns for the dataframe even if we had to change just one column name.
Method 3: Rename columns in Pandas by changing its attribute
You can also update a dataframe’s column by setting its columns
attribute to your new list of columns. The following is the syntax:
df.columns = new_column_list
Note that new_column_list
must be of the same length as the number of columns in your dataframe.
Let’s look at our final example.
Example: Update columns attribute to change column names
We’ll take the same use case as above. Create a dataframe with “Category” ad “Color” columns and then change the column name “Category” to “Pet” but this time we’ll do it by updating the columns attribute.
import pandas as pd # data of pets data = {'Category': ['Dog', 'Cat', 'Rabbit', 'Parrot'], 'Color': ['brown', 'black', 'white', 'green']} # create pandas dataframe df = pd.DataFrame(data) # print dataframe columns print("Dataframe columns:", df.columns) # change column name Category to Pet df.columns = ["Pet", "Color"] # print dataframe columns print("Dataframe columns:", df.columns)
Output:
Dataframe columns: Index(['Category', 'Color'], dtype='object') Dataframe columns: Index(['Pet', 'Color'], dtype='object')
In the above example, we change the column names of the dataframe df
by setting df.columns
to a new column list. Like the set_index()
function, we had to provide the list of all the columns for the dataframe even if we had to change just one column name.
Summary: Changing Column Names in Pandas (Key Takeaways)
In this tutorial, we looked at three different methods along with examples of application to rename columns in a Pandas dataframe. The following are the key takeaways –
- You can change Pandas dataframe column names by using the
rename()
function to change the name of a single column or to rename multiple columns. Pass a dictionary{"OldName":"NewName"}
to thecolumns
parameter. You can also pass a function to thecolumns
parameter to dynamically change the column names. - You can set the column names of the dataframe to new column names using the Pandas dataframe
setaxis()
function. Explicitly specifyaxis=1
oraxis='columns'
to indicate that you’re setting the columns’ axis. - Alternatively, you can change the column names of a dataframe by changing the
.columns
attribute of the dataframe.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
More on Pandas DataFrames –
- Pandas – Sort a DataFrame
- Change Order of Columns of a Pandas DataFrame
- Pandas DataFrame to a List in Python
- Pandas – Count of Unique Values in Each Column
- Pandas – Replace Values in a DataFrame
- Pandas – Filter DataFrame for multiple conditions
- Pandas – Random Sample of Rows
- Pandas – Random Sample of Columns
- Save Pandas DataFrame to a CSV file
- Pandas – Save DataFrame to an Excel file
- Create a Pandas DataFrame from Dictionary
- Convert Pandas DataFrame to a Dictionary
- Drop Duplicates from a Pandas DataFrame
- Concat DataFrames in Pandas
- Append Rows to a Pandas DataFrame
- Compare Two DataFrames for Equality in Pandas
- Get Column Names as List in Pandas DataFrame
- Select One or More Columns in Pandas
- Pandas – Rename Column Names
- Pandas – Drop one or more Columns from a Dataframe
- Pandas – Iterate over Rows of a Dataframe
- How to Reset Index of a Pandas DataFrame?
- Read CSV files using Pandas – With Examples
- Apply a Function to a Pandas DataFrame
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.