How to Rename Columns in Pandas Dataframe – Definitive Guide

Pandas DataFrame is a two-dimensional data structure used to store the data in rows and column format and each column will have a headers.

You can rename the column in Pandas dataframe using the df.rename( columns={“Old Column Name”:”New Column Name” } ,inplace=True) statement.

In this tutorial, you’ll learn various methods available to rename columns in Pandas DataFrame.

If You’re in Hurry…

Here is an example of how to rename a column in pandas dataframe.

df.rename(
    columns={"Old_Column_Name_1":"New_Column_Name_1",
                "Old_Column_Name_2":"New_Column_Name_2"}
          ,inplace=True)

Where Columns is a dictionary containing a list of columns to be changed or renamed.

  • Key – the old column name
  • Value – new column name
  • inplace=True – denotes to make changes in the current DataFrame directly rather than creating a new Dataframe after renaming the columns

If You Want to Understand Details, Read on…

There are different methods to rename columns in pandas dataframe. Let us learn these methods by using the below sample dataframe df.

Sample DataFrame

import pandas as pd

data = {
    "Lang":["Java","Python","Cobol","Javascript"],        
    "Difficulty":["Medium","Easy","Hard","Medium"],
    "Type":["Statically Typed","Dynamically Typed","NA","Dynamically typed"]
}

df = pd.DataFrame(data)

print(df)

DataFrame Will Look Like

         Lang Difficulty               Type
0        Java     Medium   Statically Typed
1      Python       Easy  Dynamically Typed
2       Cobol       Hard                 NA
3  Javascript     Medium  Dynamically typed

Now, let’s discuss all the methods available to rename columns in pandas.

Using rename() Method

rename() method is used to rename the columns or indexes/rows in the Pandas DataFrame.

Syntax

df.rename(columns={"Old_Column_Name_1":"New_Column_Name_1",                 "Old_Column_Name_2":"New_Column_Name_2"} ,inplace=True)

Where Columns is a dictionary containing a list of columns to be changed or renamed.

  • Key – the old column name
  • Value – new column name
  • inplace=True – denotes to make changes in the current DataFrame directly rather than creating a new Dataframe after renaming the columns

Using Columns Attribute

You can change the dataframe column names by directly assigning the column names to the DataFrame using columns attributes as below.

Syntax

df.columns = ['New_Column_1', 'New_Column_2']

This will directly assign the new column names to your dataframe.

Ensure that the list has the same number of values as the number of columns in the dataframe.

Example

#Print the dataframe before renaming
print("Dataframe before Renaming\n\n")

print(df)

#Renaming the dataframe Column Index 0
df.columns = ['Language','Complexity','Typ']

print("\n\nDataframe after Renaming using Columns Attribute\n\n")

print(df)

Output

Dataframe before Renaming
*************************


         Lang Difficulty               Type
0        Java     Medium   Statically Typed
1      Python       Easy  Dynamically Typed
2       Cobol       Hard                 NA
3  Javascript     Medium  Dynamically typed


Dataframe after Renaming using Columns Attribute
***********************************************


     Language Difficulty               Typ
0        Java     Medium   Statically Typed
1      Python       Easy  Dynamically Typed
2       Cobol       Hard                 NA
3  Javascript     Medium  Dynamically typed

Using set_axis() Method

set_axis() method can be used to set the axis or rename columns of the dataframe.

You can set the index name when creating a dataframe using this set_axis() method.

A pandas DataFrame has two axes. where

  • A column axis can be mentioned as axis='columns' or axis=1
  • A row axis can be mentioned as axis='index' or axis=0

To rename column axes using the set_Axis(), use axis =1 or axis = columns as a parameter.

Syntax

df.set_axis(['New_Column_1', 'New_Column_2'],axis=1,inplace=True)

Where

  • ['New_Column_1', 'New_Column_2'] – list of columns names to be assigned.
  • axis=1 – denotes columns to be renamed.
  • inplace=True – denotes the actual DataFrame needs to be renamed rather than creating a new DataFrame object.

Example

print("Dataframe before Renaming\n\n")

print(df)

df.set_axis(["Language","Complexity","Typ"],axis=1,inplace=True)

print("\n\nDataframe after Renaming using Columns Attribute\n\n")

print(df)

Output

 Dataframe before Renaming


             Lang Difficulty               Type
    0        Java     Medium   Statically Typed
    1      Python       Easy  Dynamically Typed
    2       Cobol       Hard                 NA
    3  Javascript     Medium  Dynamically typed


   Dataframe after Renaming using Columns Attribute


         Language Complexity               Typ
    0        Java     Medium   Statically Typed
    1      Python       Easy  Dynamically Typed
    2       Cobol       Hard                 NA
    3  Javascript     Medium  Dynamically typed

This is how you can set the column axis using the set_axis() method in the pandas dataframe.

Rename all Columns

In this section, you’ll learn how to use rename() method to change all column names in the DataFrame.

You can rename all columns in pandas DataFrame with a list or a dictionary of columns.

Syntax

df.rename(
    columns={"Old_Column_Name_1":"New_Column_Name_1",
                "Old_Column_Name_2":"New_Column_Name_2",
                   "Old_Column_Name_3":"New_Column_Name_3"}
          ,inplace=True)

Where Columns is a dictionary containing a list of columns to be changed or renamed.

  • Key should be a old column name
  • Value should be a new column name
  • inplace=True – Used to denote to make changes in the current DataFrame directory rather creating a new Dataframe for change

Example

print("Dataframe before Renaming\n\n")

print(df)


df.rename(columns={"Lang":"Language","Difficulty":"Complexity","Typ":"Type"},inplace=True)

print("\n\nDataframe after Renaming\n\n")

print(df)

Output

Dataframe before Renaming


         Lang Difficulty               Type
0        Java     Medium   Statically Typed
1      Python       Easy  Dynamically Typed
2       Cobol       Hard                 NA
3  Javascript     Medium  Dynamically typed


Dataframe after Renaming


     Language Complexity                Typ
0        Java     Medium   Statically Typed
1      Python       Easy  Dynamically Typed
2       Cobol       Hard                 NA
3  Javascript     Medium  Dynamically typed

Rename Specific Columns

In this section, you’ll learn how to rename specific column in pandas DataFrame using the rename() method.

Useful in Scenarios

  • To change a specific column
  • To rename list of columns except one column or two columns.

Syntax

df.rename(
    columns={"Old_Column_Name_1":"New_Column_Name_1"}
          ,inplace=True)

Where Columns is a dictionary containing a list of columns to be changed or renamed.

  • Key should be a old column name
  • Value should be a new column name
  • inplace=True – Used to denote to make changes in the current DataFrame directory rather creating a new Dataframe for change

Example

print("Dataframe before Renaming\n\n")

print(df)

df.rename(columns={"Lang":"Language"},inplace=True)

print("\n\nDataframe after Renaming the Language Column\n\n")

print(df)

Output

Dataframe before Renaming


         Lang Difficulty               Type
0        Java     Medium   Statically Typed
1      Python       Easy  Dynamically Typed
2       Cobol       Hard                 NA
3  Javascript     Medium  Dynamically typed


Dataframe after Renaming


     Language Difficulty                Type
0        Java     Medium   Statically Typed
1      Python       Easy  Dynamically Typed
2       Cobol       Hard                 NA
3  Javascript     Medium  Dynamically typed

You can see only the specific column Lang is replaced as Language as mentioned in the rename() method.

Rename DataFame Column by index

You can rename a column using the column index. This is also known as renaming pandas column using number or position.

Useful in Scenarios

  • Rename pandas column without knowing the column name
  • Rename pandas column dynamically

The index is 0 based. Use 0, if you want to rename the first column.

You need to use the column name in the dictionary key, by fetching the existing column name using its index as df.columns[0].

Syntax

df.rename(columns={df.columns[0]: 'New_Column_name'})

Example

print("Dataframe before Renaming")

print(df)


df.rename(columns={df.columns[0]: 'Language'},inplace=True)

print("\n\nDataframe after Renaming using column Index 0. Lang is renamed as Language\n\n")

print(df)

Output

Dataframe before Renaming



         Lang Difficulty               Type
0        Java     Medium   Statically Typed
1      Python       Easy  Dynamically Typed
2       Cobol       Hard                 NA
3  Javascript     Medium  Dynamically typed


Dataframe after Renaming using column Index 0. Lang is renamed as Language



     Language Difficulty                Type
0        Java     Medium   Statically Typed
1      Python       Easy  Dynamically Typed
2       Cobol       Hard                 NA
3  Javascript     Medium  Dynamically typed

You can see the column index 0 is replaced as Language as mentioned in the rename() method.

This is how you can rename pandas column by position.

Next, you’ll learn how to rename using the axis style.

Rename with Axis Style

You can rename the columns using the rename() method by using the axis keyword in it.

In this method, you’ll specify the columns as Python Set within { } rather specifying columns as a Python Dictionary with Key-Value Pairs.

This method can also be used to rename the rows/indexes of the Pandas DataFrame.

To rename the column axis, use axis =1 or axis = columns.

Syntax

df.rename({"Old_Column_Name_1":"New_Column_Name_2","Old_Column_Name_2":"New_Column_Name_2"},axis=1, inplace=True)

Where Columns is specified as Python Set within the braces { }.

  • axis=1 – denotes column axis to be renamed
  • inplace=True – denotes the original DataFrame needs to be renamed rather than creating a new DataFrame object after renaming the columns

Example

print("Dataframe before Renaming\n\n")

print(df)


df.rename({"Lang":"Language","Difficulty":"Complexity","Type":"Typ"},axis=1, inplace=True)

print("\n\nDataframe after Renaming using Axis Keyword\n\n")

print(df)

Output

Dataframe before Renaming


         Lang Difficulty               Type
0        Java     Medium   Statically Typed
1      Python       Easy  Dynamically Typed
2       Cobol       Hard                 NA
3  Javascript     Medium  Dynamically typed


Dataframe after Renaming using Axis Keyword


     Language Complexity                Typ
0        Java     Medium   Statically Typed
1      Python       Easy  Dynamically Typed
2       Cobol       Hard                 NA
3  Javascript     Medium  Dynamically typed

You can see all the columns of pandas DataFrame are renamed with the new names.

Next, you’ll see about the exception handling during the rename function.

Handling Errors while using rename function

In any program, exception needs to be handled appropriately to avoid the unexpected breaking of the program. In this section, we’ll show you how to handle exceptions that occur during the rename operation.

When you use rename() method, it’ll raise KeyError error, when the specified Key is not available in the DataFrame.

You can specify what needs to be done in case of such errors.

  • Use errors='raise' to raise the error.
  • Use errors='ignore' to ignore the error and perform rename
    operation for valid columns. This is the default behavior.

Syntax to Raise error

df.rename(
    {"Old_Column_Name_1":"New_Column_Name_2","Unavailable_Column":"New_Column_Name_2"},
        axis='columns', inplace=True, 
            errors='raise')

Example

print("Dataframe before Renaming")

print(df)

df.rename({"Langguage":"Language"},axis=1, inplace=True,
          errors='raise')

print("\n\nDataframe after Renaming using Axis Keyword\n\n")

print(df)

Output

Dataframe before Renaming
*************************


         Lang Difficulty               Type
0        Java     Medium   Statically Typed
1      Python       Easy  Dynamically Typed
2       Cobol       Hard                 NA
3  Javascript     Medium  Dynamically typed

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-10-5b9d494d56d1> in <module>
 15 
 16 #Renaming the dataframe Column Index 0
---> 17 df.rename({"Langguage":"Language"},axis=1, inplace=True, 18           errors='raise')
 19 

E:\ProgramData\Anaconda3\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
 307         @wraps(func)
 308         def wrapper(*args, **kwargs) -> Callable[..., Any]:
--> 309  return func(*args, **kwargs)
 310 
 311         kind = inspect.Parameter.POSITIONAL_OR_KEYWORD

E:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in rename(self, mapper, index, columns, axis, copy, inplace, level, errors)
 4294         4  3  6
 4295         """
-> 4296 return super().rename( 4297             mapper=mapper,
 4298             index=index,

E:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in rename(self, mapper, index, columns, axis, copy, inplace, level, errors)
 943                         if indexer[index] == -1
 944                     ]
--> 945  raise KeyError(f"{missing_labels} not found in axis")
 946 
 947             new_index = ax._transform_index(f, level)

KeyError: "['Langguage'] not found in axis"

Now, when you tried to rename the non-existing column Langguage, the Pandas rename() method has raised an error.

Conclusion

In this tutorial, you’ve learned various methods available to rename columns in Pandas DataFrame. Renaming Columns is also known as setting column names to Pandas Dataframe.

If you know any other methods that can be used to rename columns in pandas Dataframe, feel free to comment.

You May also Like

Leave a Comment