Given a Pandas DataFrame, let’s see how to rename columns in Pandas with examples. Here, we will discuss 6 different ways to rename column names in pandas DataFrame.
About Pandas DataFrame:
Pandas DataFrame is a rectangular grid that is used to store data. It is easy to visualize and work with data when stored in dataFrame.
- It consists of rows and columns.
- Each row is a measurement of some instance while the column is a vector that contains data for some specific attribute/variable.
- Each Dataframe column has homogeneous data throughout any specific column but Dataframe rows can contain homogeneous or heterogeneous data throughout any specific row.
- Unlike two-dimensional arrays, pandas’ Dataframe axes are labeled.
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: