Rename Columns in Pandas DataFrame

This article will discuss how to rename column names in Pandas DataFrame.

Table of Contents

A DataFrame is a data structure that will store the data in rows and columns. We can create a DataFrame using pandas.DataFrame() method. Let’s discuss the methods to rename columns in pandas DataFrame.

Rename column names using rename() method in Dataframe

This method is used to rename the column names in the DataFrame by taking an existing columns as input in a dictionary.

Syntax:

dataframe.rename(columns,inplace=True)

where,

  • dataframe is the input dataframe
  • columns parameter take a dictionary of columns to rename the columns
    • like, {‘old_column_mame’:’new_column_name’,……………,’old_column_mame’:’new_column_name’}
  • inplace is used to return the new dataframe. If it is set to True, then the copy is ignored.

Note: We can rename single or multiple columns at a time.

Let’s see the examples.

Before that We create the DataFrame. Here, we are going to create the DataFrame named data with 4 rows and 4 columns.

import pandas as pd

#create the dataframe with 4 columns
data=pd.DataFrame({'id':[7058,7069,7060,7061],
                   'name':['sravan','bobby','ojaswi','deepu'],
                   'age':[21,23,22,21],
                   'subjects':['linux','html/css','node-js','php-mysql']})

#display
print(data)

Output:

     id    name  age   subjects
0  7058  sravan   21      linux
1  7069   bobby   23   html/css
2  7060  ojaswi   22    node-js
3  7061   deepu   21  php-mysql

Rename Single Column Name in Pandas Dataframe

Here we are going to display the dataframe by renaming the single column for all the columns.

#rename id column with student_id
data.rename(columns={'id':'student_id'},inplace=True)

#rename name  column with student_name
data.rename(columns={'name':'student_name'},inplace=True)

#rename age  column with student_age
data.rename(columns={'age':'student_age'},inplace=True)

#rename subjects  column with Programming
data.rename(columns={'subjects':'Programming'},inplace=True)

#display dataframe
print(data)

Output:

   student_id student_name  student_age Programming
0        7058       sravan           21       linux
1        7069        bobby           23    html/css
2        7060       ojaswi           22     node-js
3        7061        deepu           21   php-mysql

Here we renamed,

  • id column with student_id,
  • name column with student_name,
  • age column with student_age,
  • subjects column with Programming.

Rename multiple columns Names in Pandas Dataframe

Here we are going to display the dataframe by renaming the multiple columns for all the columns at a time.

import pandas as pd

#create the dataframe with 4 columns
data=pd.DataFrame({'id':[7058,7069,7060,7061],
                   'name':['sravan','bobby','ojaswi','deepu'],
                   'age':[21,23,22,21],
                   'subjects':['linux','html/css','node-js','php-mysql']})

#display
print(data)

print('*** Rename all COlumn names *****')

#rename id column with student_id
#rename name  column with student_name
#rename age  column with student_age
#rename subjects  column with Programming
data.rename(columns={'id':'student_id',
                     'name':'student_name',
                     'age':'student_age',
                     'subjects':'Programming' },
                     inplace=True)

#display dataframe
print(data)

Output:
Here we renamed id column with student_id,name column with student_name, age column with student_age, subjects column with Programming.

     id    name  age   subjects
0  7058  sravan   21      linux
1  7069   bobby   23   html/css
2  7060  ojaswi   22    node-js
3  7061   deepu   21  php-mysql

*** Rename all COlumn names *****

   student_id student_name  student_age Programming
0        7058       sravan           21       linux
1        7069        bobby           23    html/css
2        7060       ojaswi           22     node-js
3        7061        deepu           21   php-mysql

Rename Column Names with a List in Pandas Dataframe

In this method, we are using a list that contains a new column names and then we assign this list to dataframe columns using columns method

Syntax is as follows:

dataframe.columns=['new_column1',.........,'new_column n']

where,

  • dataframe is the input dataframe
  • columns is the method used to assign columns from the list
  • List include is a list of new columns separated by comma.

Here we are going to rename columns using a list of column names

import pandas as pd

#create the dataframe with 4 columns
data=pd.DataFrame({'id':[7058,7069,7060,7061],
                   'name':['sravan','bobby','ojaswi','deepu'],
                   'age':[21,23,22,21],
                   'subjects':['linux','html/css','node-js','php-mysql']})

#display
print(data)

print('*** Rename all Column names in Dataframe *****')

#rename id column with student_id
#rename name  column with student_name
#rename age  column with student_age
#rename subjects  column with Programming
data.columns=['student_id','student_name','student_age','Programming']

#display dataframe
print(data)


Output:

     id    name  age   subjects
0  7058  sravan   21      linux
1  7069   bobby   23   html/css
2  7060  ojaswi   22    node-js
3  7061   deepu   21  php-mysql

*** Rename all Column names in Dataframe *****

   student_id student_name  student_age Programming
0        7058       sravan           21       linux
1        7069        bobby           23    html/css
2        7060       ojaswi           22     node-js
3        7061        deepu           21   php-mysql

Here we renamed id column with student_id,name column with student_name, age column with student_age, subjects column with Programming.

Change Column Names in Pandas Dataframe using set_axis()

This method will rename the columns of the DataFrame by using axis. In this method, we are passing a list that contains a new column names as first parameter and specify column axis i.e axis=1

Syntax:

data.set_axis(['new_column1',.............,'new_column n'], axis=1)

where,

  • dataframe is the input dataframe
  • list of new_columns is the first parameter
  • axis=1 specifies the column

Here we are going to rename columns using a list of column names

import pandas as pd

#create the dataframe with 4 columns
data=pd.DataFrame({'id':[7058,7069,7060,7061],
                   'name':['sravan','bobby','ojaswi','deepu'],
                   'age':[21,23,22,21],
                   'subjects':['linux','html/css','node-js','php-mysql']})

#display
print(data)

print('*** Rename all Column names in Dataframe *****')

#rename id column with student_id
#rename name  column with student_name
#rename age  column with student_age
#rename subjects  column with Programming
data.set_axis(['student_id','student_name','student_age','Programming'],axis=1)

#display dataframe
print(data)

Output:

     id    name  age   subjects
0  7058  sravan   21      linux
1  7069   bobby   23   html/css
2  7060  ojaswi   22    node-js
3  7061   deepu   21  php-mysql

*** Rename all Column names in Dataframe *****

     id    name  age   subjects
0  7058  sravan   21      linux
1  7069   bobby   23   html/css
2  7060  ojaswi   22    node-js
3  7061   deepu   21  php-mysql

Here we renamed id column with student_id, name column with student_name, age column with student_age, subjects column with Programming.

Rename Column Names in Dataframe using str.replace()

This method is used to rename the old column name with new column name

In Pandas, we are using columns method along with this method to rename single column at a time

Syntax:

dataframe.columns.str.replace('old_column_name', 'new_column_name')

where,

  • dataframe is the input dataframe
  • old_column_name is the existing column and new_column_name is the replaced column

Here we are going to rename columns one by one

import pandas as pd

#create the dataframe with 4 columns
data=pd.DataFrame({'id':[7058,7069,7060,7061],
                   'name':['sravan','bobby','ojaswi','deepu'],
                   'age':[21,23,22,21],
                   'subjects':['linux','html/css','node-js','php-mysql']})

#display
print(data)

print('*** Rename all Column names in Dataframe one by one *****')

#rename id column with student_id
data.columns = data.columns.str.replace('id', 'student_id')

#rename name  column with student_name
data.columns = data.columns.str.replace('name', 'student_name')

#rename age  column with student_age
data.columns = data.columns.str.replace('age', 'student_age')

#rename subjects  column with Programming
data.columns = data.columns.str.replace('subjects', 'Programming')

#display dataframe
print(data)

Output:

     id    name  age   subjects
0  7058  sravan   21      linux
1  7069   bobby   23   html/css
2  7060  ojaswi   22    node-js
3  7061   deepu   21  php-mysql

*** Rename all Column names in Dataframe one by one *****

   student_id student_name  student_age Programming
0        7058       sravan           21       linux
1        7069        bobby           23    html/css
2        7060       ojaswi           22     node-js
3        7061        deepu           21   php-mysql

Here we renamed id column with student_id, name column with student_name, age column with student_age, subjects column with Programming and displayed the column names

Summary

In this article we discussed four methods for renaming the column in pandas DataFrame with examples.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top