Renaming Columns in Pandas
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.
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
Best-suited Python courses for you
Learn Python with these high-rated online courses
Create a DataFrame
Input
#import Pandasimport pandas as pd
#create a dataframefaculties = 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 dataframeprint (faculties)
Output
We will now use the above data frame (faculties) to better understand renaming columns in pandas.
Also Read: Series vs. DataFrame in Pandas
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 columnsprint("Name of New Columns:", faculties.columns)
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 columnsprint("Name of New Columns:", faculties.columns)
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 columnsprint("Name of New Columns:", faculties.columns)faculties.head()
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 columnsprint("Name of New Columns:", faculties.columns)faculties.head()
- 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 columnsprint("Name of New Columns:", faculties.columns)faculties.head()
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!!
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