If your are looking for the answer “how to rename columns in Pandas DataFrame”. So you are at the right place. This article covers the answer to your question. In this article we are going to cover different ways or methods to answer the question “rename column in pandas”. Let’s see how to rename column from in Pandas.

It’s normal practice to rename dataframe columns, especially when we want to share some insights with other people and teams. This suggests that we might want to give column names additional significance so that readers can relate them to particular situations more readily.

Lets discuses best methods to rename column in pandas:


Rename columns in Pandas DataFrame

Method 1: By using  rename() method of the DataFrame

The rename() function is used to alter axes labels. Function / dict values must be unique (1-to-1). Labels not contained in a dict / Series will be left as-is. Extra labels listed don’t throw an error.

# Import pandas package
import pandas as pd
  
# Define a dictionary containing students information
data = {'Name': ['Rahul', 'Pooja',
                'Ketan', 'Mili'],
        'RollNo': [101, 102, 103, 104],
        'Subject': ['Hindi', 'English',
                'Math', 'Science'],
        'Marks': [79, 85, 89, 95]}

# Convert the dictionary into DataFrame
df = pd.DataFrame(data, columns=['Name', 'RollNo',
                                'Subject', 'Marks'])
print("Given Dataframe :\n", df)
  
# Rename a column name 'Marks' to Percentage
df.rename (columns = {'Marks':'Percentage'}, inplace=True )

# You can change any number of columns like :
# df.rename (columns = {'Marks':'Percentage' , 'Subject':'Stream'}, inplace=True )
# After rename
print("After Rename a column Dataframe is :\n", df)

Output:

After Rename a column Dataframe is :
       Name    RollNo     Subject       Percentage
0      Rahul      101       Hindi          79
1      Pooja      102     English          85
2      Ketan      103        Math          89
3      Mili       104     Science          95

Method 2: Rename All Columns

The columns can also be changed by directly changing the names of the columns by setting a list containing the new names to the columns attribute of the Dataframe object. The drawback of this approach is that, even if we only wish to rename part of the columns, we must offer new names for all of them.

# Import pandas package
import pandas as pd
  
# Define a dictionary containing students information
data = {'Name': ['Rahul', 'Pooja',
                'Ketan', 'Mili'],
        'RollNo': [101, 102, 103, 104],
        'Subject': ['Hindi', 'English',
                'Math', 'Science'],
        'Marks': [79, 85, 89, 95]}

# Convert the dictionary into DataFrame
df = pd.DataFrame(data, columns=['Name', 'RollNo',
                                'Subject', 'Marks'])
print("Given Dataframe :\n", df)
  
# Rename all columns
df.columns = ['Col1', 'Col2', 'Col3', 'Col4']

# After rename
print("After Rename all columns Dataframe is :\n", df)

Output:

After Rename all columns Dataframe is :
        Col1     Col2        Col3        Col4
0      Rahul      101       Hindi          79
1      Pooja      102     English          85
2      Ketan      103        Math          89
3      Mili       104     Science          95

Method 3: Rename All Columns by using DataFrame set_axis() function

The set_axis() function is used to assign desired index to given axis. Indexes for column or row labels can be changed by assigning a list-like or Index.

# Import pandas package
import pandas as pd
  
# Define a dictionary containing students information
data = {'Name': ['Rahul', 'Pooja',
                'Ketan', 'Mili'],
        'RollNo': [101, 102, 103, 104],
        'Subject': ['Hindi', 'English',
                'Math', 'Science'],
        'Marks': [79, 85, 89, 95]}

# Convert the dictionary into DataFrame
df = pd.DataFrame(data, columns=['Name', 'RollNo',
                                'Subject', 'Marks'])
print("Given Dataframe :\n", df)
  
# Rename all columns
df.set_axis(['A', 'B', 'C', 'D'], axis='columns', inplace=True)

# After rename
print("After Rename all columns Dataframe is :\n", df)

Output:

After Rename all columns Dataframe is :
         A         B          C            D
0      Rahul      101       Hindi          79
1      Pooja      102     English          85
2      Ketan      103        Math          89
3      Mili       104     Science          95

Method 4: Replace specific texts of column names by using Dataframe.columns.str.replace function

You can also replace a specific tests of column names with the help of Dataframe.columns.str.replace function. Let’s check how this works?

# Import pandas package
import pandas as pd
  
# Define a dictionary containing students information
data = {'Name': ['Rahul', 'Pooja',
                'Ketan', 'Mili'],
        'RollNo': [101, 102, 103, 104],
        'Subject': ['Hindi', 'English',
                'Math', 'Science'],
        'Marks': [79, 85, 89, 95]}

# Convert the dictionary into DataFrame
df = pd.DataFrame(data, columns=['Name', 'RollNo',
                                'Subject', 'Marks'])
print("Given Dataframe :\n", df)
  
# Rename Specific text of column names
df.columns = df.columns.str.replace('Marks', 'Percentage')
df.columns = df.columns.str.replace('Subject', 'Stream')

# After rename
print("After Rename specific columns Dataframe is :\n", df)

Output:

After Rename specific columns Dataframe is :
       Name       RollNo    Stream         Percentage
0      Rahul      101       Hindi          79
1      Pooja      102     English          85
2      Ketan      103        Math          89
3      Mili       104     Science          95

Final Thoughts about ‘rename column in pandas’

This article examined several approaches of renaming columns in a Pandas dataframe. The main takeaways are as follows:

  • Use the pandas dataframe rename() function to change the name of one or more columns. By passing {"OldName":"NewName"} to the columns parameter. You can also pass a function to the columns parameter to dynamically change the column names.
  • You can set the column names of the dataframe to new column names using the pandas dataframe setaxis() function. Explicitly specify axis=1 or axis='columns' to indicate that you’re setting the columns’ axis.
  • You can change the column names of a dataframe by changing the .columns attribute of the dataframe.
  • You can also replace a specific tests of column names with the help of Dataframe.columns.str.replace function.