Renaming Columns in Pandas

Renaming Columns in Pandas

3 mins read530 Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Oct 3, 2023 11:46 IST

Renaming Columns in Pandas is one of the basic but most important processes while doing data analysis or cleaning to give a meaningful name so that anyone can easily understand the data. In this article, we will discuss different methods of renaming columns in Pandas with the help of examples.

2022_10_MicrosoftTeams-image-54.jpg

When the raw data is imported into pandas for analysis, sometimes it has messy names, so you need to rename the columns with a meaningful name. Pandas offer different methods to rename the name of columns/indexes, such as rename (), add_prefix (), add_suffix (), set_axis (). This article will briefly discuss different methods of renaming columns in Pandas.

Must Check: Pandas Online Courses and Certifications

Must Check: Pandas Interview Questions for Data Scientists

Table of Content

Before discussing different methods of renaming columns in pandas, let’s set up the data first, i.e., we will create a data frame to perform the actions on it.

Also Read: How to read and write files using Pandas

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

– / –
40 hours
– / –
5 days
– / –
3 days
3 K
3 weeks
– / –
4 days
– / –
20 hours
– / –
2 months
Free
6 weeks

Create a DataFrame

Input


 
#import Pandas
import pandas as pd
#create a dataframe
faculties = pd.DataFrame({'Name' : ['Ramanujan', 'CV Raman', 'Tagore', 'CNR Rao', 'JC Bose'],
'Department' : ['Mathematics', 'Physics', 'Literature', 'Chemistry', 'Biology'],
'Subject' : ['Integral Calculus', 'Optics', 'Bengali Literature', 'Solid State Chemistry & Catalyst', 'Plants'],
'Number of Class' : [3, 2, 1, 2, 3]})
#print dataframe
print (faculties)
Copy code

Output

We will now use the above data frame (faculties) to better understand renaming columns in pandas. 

Also Read: Series vs. DataFrame in Pandas

Enumerate in Python
Introduction to Iterators in Python
Working with Python Datetime Module

Methods of Renaming Columns

rename () function

rename () function in pandas is used when we need to make changes in selected columns since we have to specify the name of the columns that have to be renamed.

Example: Change the column’s name from ‘Name’ to ‘Professor.’

Input


 
# using DataFrame.rename() function
faculties.rename(columns = {'Name': 'Professor'}, inplace = True)
#print the name of new columns
print("Name of New Columns:", faculties.columns)
Copy code
How to use pass statement in Python
Pattern Programs in Python
Memory Management in Python

set_axis() function

set_axis() function assigns the required index to either a column or index axis.

To rename the column name set:

  • axis = ‘columns’ or
  • axis = 1

Example: Change the column’s name from Name Department, Subject, and Number of class to A, B, C, and D.

Input


 
# using DataFrame.rename() function
faculties.set_axis(['A', 'B', 'C', 'D'], axis='columns', inplace=True)
#print the name of new columns
print("Name of New Columns:", faculties.columns)
Copy code
Abstraction in Python
Introduction to Recursion Functions in Python
Python Sorted() Function

add_prefix(), add_sufix() function

We can also change the column’s name by adding relevant prefixes (start of the column name) and suffixes (end of the column name).

Example: Add ‘column_name:’ and ‘.’ before the start and end of the given column name, respectively.

Input


 
#using DataFrame.add_prefix and DataFrame.add_suffix
faculties = faculties.add_prefix('column_name: ')
faculties = faculties.add_suffix('.')
#print the name of new columns
print("Name of New Columns:", faculties.columns)
faculties.head()
Copy code

Output

Also Read: Data Cleaning using Pandas.

Ternary Operators in Python
How to Convert Celsius to Fahrenheit in Python
Rock Paper Scissors Game in Python

dataframe.column() function

  • Rename all columns

We can rename all the columns of any DataFrame by the rename () function and the set_axis() function. But using dataframe.column (), we can directly rename the names of columns.

  • Here, we have to assign a list of name corresponding to the name of the column that we need to change.
  • The only limitation of this method is that it changes the name of all columns, even if we only want to change in fewer columns.

Example: Change the column’s name from Name Department, Subject, and Number of class to col_A, col_B, col_C, and col_D.

Input


 
#using DataFrame.columns()
faculties.columns = ['col_A', 'col_B', 'col_C', 'col_D']
#print the name of new columns
print("Name of New Columns:", faculties.columns)
faculties.head()
Copy code

Output

Also Read: Sorting Data using Pandas.

How to Check if Two Python Strings are Anagrams
Python Program to Swap Two Variables
Dice Roll Generator in Python
  • Replace specific characters in columns

Till now, we have learned how to rename the columns in pandas using different methods. But how will you do that if we have to remove any specific character or word in the columns’ names?

Let’s make it simple; from the above example, remove (or replace) ‘col’ from the name of each column.

So, to do that, we will use DataFrame.columns.str.replace() function

Input


 
#using DataFrame.columns.str.replace()
faculties.columns = faculties.columns.str.replace('col', '')
#print the name of new columns
print("Name of New Columns:", faculties.columns)
faculties.head()
Copy code
Free QR Code Generator using Python
Python Program to Check Leap Year
Find the Second Occurrence of a Substring in Python String

Conclusion

In this article, we have briefly discussed different methods of renaming columns in pandas with the help of examples.

Hope, you will like the article.

Keep Learning!!

Keep Sharing!!

Handwritten Digit Recognition with 98% Accuracy
Top 110+ Python Interview Questions and Answers
About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio