How to rename columns in Pandas DataFrame

01 Jul, 2022
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.
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 packageimport pandas as pd  # Define a dictionary containing ICC rankingsrankings = {'test': ['India', 'South Africa', 'England',                            'New Zealand', 'Australia'],            'odi': ['England', 'India', 'New Zealand',                            'South Africa', 'Pakistan'],            't20': ['Pakistan', 'India', 'Australia',                            'England', 'New Zealand']}  # Convert the dictionary into DataFramerankings_pd = pd.DataFrame(rankings)  # Before renaming the columnsprint(rankings_pd)  rankings_pd.rename(columns = {'test':'TEST'}, inplace = True)  # After renaming the columnsprint("\nAfter modifying first column:\n", rankings_pd.columns)
Output: 
 
Example 2: Rename multiple columns. 
Python3
# Import pandas packageimport pandas as pd   # Define a dictionary containing ICC rankingsrankings = {'test': ['India', 'South Africa', 'England',                            'New Zealand', 'Australia'],              'odi': ['England', 'India', 'New Zealand',                            'South Africa', 'Pakistan'],               't20': ['Pakistan', 'India', 'Australia',                              'England', 'New Zealand']}   # Convert the dictionary into DataFramerankings_pd = pd.DataFrame(rankings)   # Before renaming the columnsprint(rankings_pd.columns)   rankings_pd.rename(columns = {'test':'TEST', 'odi':'ODI',                              't20':'T20'}, inplace = True)   # After renaming the columnsprint(rankings_pd.columns)
Output: 
 
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 packageimport pandas as pd  # Define a dictionary containing ICC rankingsrankings = {'test': ['India', 'South Africa', 'England',                            'New Zealand', 'Australia'],              'odi': ['England', 'India', 'New Zealand',                            'South Africa', 'Pakistan'],               't20': ['Pakistan', 'India', 'Australia',                              'England', 'New Zealand']}  # Convert the dictionary into DataFramerankings_pd = pd.DataFrame(rankings)  # Before renaming the columnsprint(rankings_pd.columns)  rankings_pd.columns = ['TEST', 'ODI', 'T-20']  # After renaming the columnsprint(rankings_pd.columns)
Output: 
 
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 packageimport pandas as pd  # Define a dictionary containing ICC rankingsrankings = {'test': ['India', 'South Africa', 'England',                     'New Zealand', 'Australia'],            'odi': ['England', 'India', 'New Zealand',                    'South Africa', 'Pakistan'],            't20': ['Pakistan', 'India', 'Australia',                    'England', 'New Zealand']}  # Convert the dictionary into DataFramerankings_pd = pd.DataFrame(rankings)  # Before renaming the columnsprint(rankings_pd.columns)  rankings_pd.set_axis(['A', 'B', 'C'], axis='columns', inplace=True)  # After renaming the columnsprint(rankings_pd.columns)rankings_pd.head()
Output:
 
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 packageimport pandas as pd  # Define a dictionary containing ICC rankingsrankings = {'test': ['India', 'South Africa', 'England',                     'New Zealand', 'Australia'],            'odi': ['England', 'India', 'New Zealand',                    'South Africa', 'Pakistan'],            't20': ['Pakistan', 'India', 'Australia',                    'England', 'New Zealand']}  # Convert the dictionary into DataFramerankings_pd = pd.DataFrame(rankings)  # Before renaming the columnsprint(rankings_pd.columns)  rankings_pd = rankings_pd.add_prefix('col_')rankings_pd = rankings_pd.add_suffix('_1')  # After renaming the columnsrankings_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
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 packageimport pandas as pd  # Define a dictionary containing ICC rankingsrankings = {'test': ['India', 'South Africa', 'England',                     'New Zealand', 'Australia'],            'odi': ['England', 'India', 'New Zealand',                    'South Africa', 'Pakistan'],            't20': ['Pakistan', 'India', 'Australia',                    'England', 'New Zealand']}  # Convert the dictionary into DataFramerankings_pd = pd.DataFrame(rankings)  # Before renaming the columnsprint(rankings_pd.columns)# df = rankings_pd  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:
 
surajkumarguptaintern
pandas-dataframe-program
Picked
Python pandas-dataFrame
Python-pandas
Technical Scripter 2018
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.