Rename Column in Pandas – Definitive Guide

Pandas is a powerful, fast, flexible, and easy to use data analysis tool developed on top of Python programming language.

Pandas DataFrame is a two-dimensional data structure used to store the data in the tabular format. It is similar to spreadsheets or a database table. DataFrame columns can be identified using its headers or its indexes.

You can rename the column in Pandas dataframe using the df.rename( columns={“Old_Column_Name_1”:“New_Column_Name_1” } ,inplace=True) method.

In this tutorial, you’ll learn various methods available to rename columns in Pandas DataFrame and assign column name to DataFrame while creating it. Renaming Columns is also known as setting column names to Pandas Dataframe.

If You’re in Hurry…

Here is the example

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

If You Want to Understand Details, Read on…

Throughout this tutorial, you’ll use the DataFrame df created by using the dictionary data.

Creating 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.

rename method is used to change column names in pandas in various scenarios as below.

  • Rename all columns of DataFrame
  • Rename specific columns DataFrame
  • Rename DataFame column by index
  • Rename using Axis Type

Each method is explained below.

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 column axis, use axis =1 or axis = columns.

To rename the index axis or row axis, use axis=0 or axis=index.

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 is to denote columns to be renamed.
  • inplace=True is to denote the original DataFrame needs to be renamed rather creating a new DataFrame object.

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.

Using Columns Attribute

You can change the column name of the Dataframe by directly assigning the column name to the DataFrame .columns attributes.

Syntax

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

This will directly assign the new column names to your 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 axis using the set_Axis(), use axis =1 or axis = columns as a parameter.

To rename index axis or row axis using the set_Axis(), use axis=0 or axis=index 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 is to denote columns to be renamed.
  • inplace=True is to denote the actual DataFrame needs to be renamed rather 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)
 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.

Assigning Column Names while creating DataFrame

In this section, you’ll learn how to assign column name to Pandas DataFrame while creating it.

Useful in Scenarios

  • When you read a CSV file and want to ignore the header row and use your own column names in the DataFrame.

Consider the data below is available in the csv file called Languages.csv.

CSV Data Looks like..

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

Now, you can read this Languages.csv file by ignoring the header and assign the columns to the DataFrame using the below example.

Example

columns = ['Language', 'Complexity', 'Typ']
df = pd.read_csv('Languages.csv', header = None, names = columns)
print(df)

Output

     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 assign a column name to DataFrame while creating it.

Conclusion

Column Names in the DataFrame play a vital role in accessing the DataFrame values for various data operations.

It’ll be very useful to rename the columns as and when required to make it easy to perform the operation.

In this tutorial, you’ve learned various methods available to rename columns in the Pandas DataFrame.

These methods can be used to set column names to pandas dataframe while reading the data from the CSV file.

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