

Pandas – Rename Column Names in Dataframe
source link: https://thispointer.com/pandas-rename-column-names-in-dataframe/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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:
Advertisements

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.
Advertisements
Recommend
-
121
Count number of Zeros in Pandas Dataframe Column This article will discuss how to count the number of zeros in a single or all column of a Pandas Dataframe. Let’s first create a Dataframe from a list of...
-
10
Pandas | Count non-zero values in Dataframe Column This article will discuss how to count the number of non-zero values in one or more Dataframe columns in Pandas. Let’s first create a Dataframe from a...
-
14
Pandas – Count True Values in a Dataframe Column In this article, we will discuss different ways to count True values in a Dataframe Column. First of all, we will create a Dataframe from a list of tuples...
-
12
How to Drop Index Column of a Pandas DataFrame – thisPointer In this article, we will discuss about the different ways to drop the Index Column of a Pandas DataFrame. A DataFrame is a data structure that stores the data in rows...
-
12
Check if a Column exists in Pandas DataFrame In this article, we will discuss how to check if a column or multiple columns exist in a Pandas DataFrame or not. Suppose we have a DataFrame, ...
-
8
In this article, we will discuss different ways to delete a column from a DataFrame in Pandas. Table Of Contents What is Pandas DataFrame? Pandas DataFrame is a labelled two di...
-
12
Convert the Column type from String to Datetime format in Pandas DataframeConvert the Column type from String to Datetime format in Pandas Dataframe10 Views30/05/2022...
-
6
Add Column to Pandas DataFrame with constant value In this article, we will learn different ways to add a column in a DataFrame with a constant value. Table Of Contents Sup...
-
10
In this article, we will discuss multiple ways to rename a DataFrame index in pandas. Table of Contents Preparing DataSet To quickly get started, let’s create a sample dataframe...
-
12
January 13, 2023 /
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK