This article will discuss how to change column names in a Pandas Dataframe in Python.
Table Of Contents
- Change Column Names in Dataframe using rename() method
- Change Column Names in Dataframe using columns attribute with list
- Change Column Names in Dataframe using set_axis()
- Change Column names in Dataframe using str.replace()
A DataFrame is a data structure that stores the data in rows and columns. We can create a DataFrame using pandas.DataFrame() method. Let’s discuss the techniques to rename columns in a Pandas Dataframe.
Change Column Names in Dataframe using rename() method
In Pandas, the Dataframe provides a function rename(), which accepts a dictionary as an argument. This dictionary contains the mapping of old column names with the new column names. It replaces the old column names with the new ones in the Dataframe.
Syntax is as follows:
df.rename(columns,inplace=True)
where,
- df is the input dataframe
- columns parameter take a dictionary of old and new column names.
- {‘old_column_name’:’new_column_name’,……………,’old_column_name’:’new_column_name’}
- inplace is used to make the changes in current Dataframe
Note: We can rename single or multiple columns at a time.
Frequently Asked:
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 four columns df = 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 The Dataframe print(df)
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
Here we are going to display the dataframe by renaming the single column for all the columns.
# Rename id column with student_id df.rename(columns={'id':'student_id'}, inplace=True) # Rename name column with student_name df.rename(columns={'name':'student_name'}, inplace=True) # Rename age column with student_age df.rename(columns={'age':'student_age'}, inplace=True) # Rename subjects column with Programming df.rename(columns={'subjects':'Programming'}, inplace=True) # Display the Dataframe print(df)
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
Here we are going to display the dataframe by renaming the multiple columns at a time.
# Rename id column with student_id # Rename name column with student_name # Rename age column with student_age # Rename subjects column with Programming df.rename(columns={ 'id':'student_id', 'name':'student_name', 'age':'student_age', 'subjects':'Programming'}, inplace=True) # Display the Dataframe print(df)
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.
Change Column Names in Dataframe using columns attribute with list
In this method, we are using a list that contains a new column names and then we assign this list to dataframe’s columns attribute. It will replace the old column names with new one,
Syntax is as follows:
dataframe.columns=['new_column1',.........,'new_column n']
Here we are going to rename columns using a list of column names
import pandas as pd # Create the dataframe with four columns df = 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 The Dataframe print(df) # Rename id column with student_id # Rename name column with student_name # Rename age column with student_age # Rename subjects column with Programming df.columns=['student_id','student_name','student_age','Programming'] # Display the Dataframe print(df)
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 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 Dataframe using set_axis()
This method will rename the columns of the DataFrame by using set_axis(). In this method, we pass a list containing new column names as the first parameter and as another parameter specify column axis i.e axis=1.
Syntax is as follows:
dataframe.set_axis(['new_column1',.............,'new_column n'], axis=1)
Where, dataframe is the input dataframe. Pass a list of column names as argument in set_axis() function. Also parameter axis=1 specifies the column axis.
Here we are going to rename columns using a list of column names
import pandas as pd # Create the dataframe with four columns df = 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 The Dataframe print(df) # Rename id column with student_id # Rename name column with student_name # Rename age column with student_age # Rename subjects column with Programming df = df.set_axis([ 'student_id', 'student_name', 'student_age', 'Programming'], axis=1) # Display The Dataframe print(df)
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 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 Dataframe using str.replace()
We can str.replace() method is used to rename the old column name with the new column name. In Pandas, we use the columns attribute along with str.replace() to rename a single column at a time.
Syntax is as follows:
dataframe.columns.str.replace('old_column_name', 'new_column_name') where, 1. dataframe is the input dataframe 2. 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 four columns df = 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 The Dataframe print(df) # Rename id column with student_id df.columns = df.columns.str.replace('id', 'student_id') # Rename name column with student_name df.columns = df.columns.str.replace('name', 'student_name') # Rename age column with student_age df.columns = df.columns.str.replace('age', 'student_age') # Rename subjects column with Programming df.columns = df.columns.str.replace('subjects', 'Programming') # Display The Dataframe print(df)
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 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.
Summary
In this article we discussed four methods for changing the column names in Pandas DataFrame with examples.