How to rename columns in Pandas DataFrame
Last Updated :
04 Dec, 2023
Given a Pandas DataFrame, let’s see how to rename columns in Pandas with examples. Here, we will discuss 5 different ways to rename column names in pandas DataFrame.
How to rename columns in Pandas DataFrame
Method 1: Using rename() function
One way of renaming the columns in a Pandas Dataframe is by using the rename() function. This method is quite useful when we need to rename some selected columns because we need to specify information only for the columns which are to be renamed.
Example 1: Rename a single column.
Python3
import pandas as pd
rankings = { 'test' : [ 'India' , 'South Africa' , 'England' ,
'New Zealand' , 'Australia' ],
'odi' : [ 'England' , 'India' , 'New Zealand' ,
'South Africa' , 'Pakistan' ],
't20' : [ 'Pakistan' , 'India' , 'Australia' ,
'England' , 'New Zealand' ]}
rankings_pd = pd.DataFrame(rankings)
print (rankings_pd)
rankings_pd.rename(columns = { 'test' : 'TEST' }, inplace = True )
print ( "\nAfter modifying first column:\n" , rankings_pd.columns)
|
Output:
Example 2: Rename multiple columns.
Python3
import pandas as pd
rankings = { 'test' : [ 'India' , 'South Africa' , 'England' ,
'New Zealand' , 'Australia' ],
'odi' : [ 'England' , 'India' , 'New Zealand' ,
'South Africa' , 'Pakistan' ],
't20' : [ 'Pakistan' , 'India' , 'Australia' ,
'England' , 'New Zealand' ]}
rankings_pd = pd.DataFrame(rankings)
print (rankings_pd.columns)
rankings_pd.rename(columns = { 'test' : 'TEST' , 'odi' : 'ODI' ,
't20' : 'T20' }, inplace = True )
print (rankings_pd.columns)
|
Output:
Method 2: By assigning a list of new column names
The columns can also be renamed by directly assigning a list containing the new names to the columns attribute of the Dataframe object for which we want to rename the columns. The disadvantage of this method is that we need to provide new names for all the columns even if want to rename only some of the columns.
Python3
import pandas as pd
rankings = { 'test' : [ 'India' , 'South Africa' , 'England' ,
'New Zealand' , 'Australia' ],
'odi' : [ 'England' , 'India' , 'New Zealand' ,
'South Africa' , 'Pakistan' ],
't20' : [ 'Pakistan' , 'India' , 'Australia' ,
'England' , 'New Zealand' ]}
rankings_pd = pd.DataFrame(rankings)
print (rankings_pd.columns)
rankings_pd.columns = [ 'TEST' , 'ODI' , 'T-20' ]
print (rankings_pd.columns)
|
Output:
Method 3: Rename column names using DataFrame set_axis() function
In this example, we will rename the column name using the set_axis function, we will pass the new column name and axis that should be replaced with a new name in the column as a parameter.
Python3
import pandas as pd
rankings = { 'test' : [ 'India' , 'South Africa' , 'England' ,
'New Zealand' , 'Australia' ],
'odi' : [ 'England' , 'India' , 'New Zealand' ,
'South Africa' , 'Pakistan' ],
't20' : [ 'Pakistan' , 'India' , 'Australia' ,
'England' , 'New Zealand' ]}
rankings_pd = pd.DataFrame(rankings)
print (rankings_pd.columns)
rankings_pd.set_axis([ 'A' , 'B' , 'C' ], axis = 'columns' , inplace = True )
print (rankings_pd.columns)
rankings_pd.head()
|
Output:
Method 4: Rename column names using DataFrame add_prefix() and add_suffix() functions
In this example, we will rename the column name using the add_Sufix and add_Prefix function, we will pass the prefix and suffix that should be added to the first and last name of the column name.
Python3
import pandas as pd
rankings = { 'test' : [ 'India' , 'South Africa' , 'England' ,
'New Zealand' , 'Australia' ],
'odi' : [ 'England' , 'India' , 'New Zealand' ,
'South Africa' , 'Pakistan' ],
't20' : [ 'Pakistan' , 'India' , 'Australia' ,
'England' , 'New Zealand' ]}
rankings_pd = pd.DataFrame(rankings)
print (rankings_pd.columns)
rankings_pd = rankings_pd.add_prefix( 'col_' )
rankings_pd = rankings_pd.add_suffix( '_1' )
rankings_pd.head()
|
Output:
col_test_1 col_odi_1 col_t20_1
0 India England Pakistan
1 South Africa India India
2 England New Zealand Australia
3 New Zealand South Africa England
4 Australia Pakistan New Zealand
Method 5: Replace specific texts of column names using Dataframe.columns.str.replace function
In this example, we will rename the column name using the replace function, we will pass the old name with the new name as a parameter for the column.
Python3
import pandas as pd
rankings = { 'test' : [ 'India' , 'South Africa' , 'England' ,
'New Zealand' , 'Australia' ],
'odi' : [ 'England' , 'India' , 'New Zealand' ,
'South Africa' , 'Pakistan' ],
't20' : [ 'Pakistan' , 'India' , 'Australia' ,
'England' , 'New Zealand' ]}
rankings_pd = pd.DataFrame(rankings)
print (rankings_pd.columns)
rankings_pd.columns = rankings_pd.columns. str .replace( 'test' , 'Col_TEST' )
rankings_pd.columns = rankings_pd.columns. str .replace( 'odi' , 'Col_ODI' )
rankings_pd.columns = rankings_pd.columns. str .replace( 't20' , 'Col_T20' )
rankings_pd.head()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...