Given a Pandas DataFrame, let’s see how to rename column names.
About Pandas DataFrame:
Pandas DataFrame are rectangular grids which are 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 column is a vector which contains data for some specific attribute/variable.
- Each dataframe column has a homogeneous data throughout any specific column but dataframe rows can contain homogeneous or heterogeneous data throughout any specific row.
- Unlike two dimensional array, pandas dataframe axes are labeled.
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.
Rename a single column.
# 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:
Rename multiple column.
# 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:
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 with this method is that we need to provide new names for all the columns even if want to rename only some of the columns.
# 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:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


