How to Rename Multiple Columns Using Pandas

Pulamolu Sai Mohan Feb 02, 2024
  1. Use the rename() Function to Rename Multiple Columns Using Pandas
  2. Use DataFrame.column.values to Rename Multiple Columns Using Pandas
How to Rename Multiple Columns Using Pandas

DataFrame is a 2-dimensional labeled data structure. It is a size-mutable and heterogeneous data structure.

The DataFrame contains labeled axes called rows and columns.

This tutorial will discuss different methods to rename multiple columns of a DataFrame using python.

Use the rename() Function to Rename Multiple Columns Using Pandas

The Pandas library provides the rename() function used to rename the columns of a DataFrame.

The rename() function takes a mapper, a dictionary-like data structure that contains the renaming column as the key and the name as the value. It returns a DataFrame.

Inplace modification can also be done by setting inplace = True.

Syntax:

pandas.rename(mapper)

Below are the steps to rename multiple columns using the rename() method.

  • Import the Pandas library.
  • Pass the Mapper to the rename() method.
  • The rename() method will return a data frame with that column renamed.

The following code is the implementation of the above approach.

# importing pandas library
import pandas as pd

# creating a dataframe
df = pd.DataFrame(
    {
        "course": ["C", "Python", "Java"],
        "Mentor": ["alex", "alice", "john"],
        "cost": [1000, 2000, 3000],
    }
)

# Dataframe before renaming
print("\n Before Renaming")
print(df)

# renaming the multiple columns by index
df = df.rename(columns={df.columns[0]: "subject", df.columns[2]: "price"})

# Dataframe after renaming
print("\n After Renaming")
print(df)

Output Before Renaming:

course Mentor cost
C alex 1000
Python alice 2000
Java john 3000

Output After Renaming:

subject Mentor price
C alex 1000
Python alice 2000
Java john 3000

Use DataFrame.column.values to Rename Multiple Columns Using Pandas

The DataFrame.column.values will return all the column names, and we can use the index to modify the column names. The column.values will return an array of an index.

Below are the steps to rename multiple columns using this approach:

  1. Import the Pandas library.
  2. Retrieve the array of column names using DataFrame.column.values.
  3. Change the name of the column by passing the index.
  4. Print the DataFrame.

The following code is the implementation of the above approach.

# importing pandas library
import pandas as pd

# creating a dataframe
df = pd.DataFrame(
    {
        "course": ["C", "Python", "Java"],
        "Mentor": ["alex", "alice", "john"],
        "cost": [1000, 2000, 3000],
    }
)

# Dataframe before renaming
print("\n Before Renaming")
print(df)

# renaming the multiple columns by index
df.columns.values[0:2] = ["Subject", "Teacher"]

# Dataframe after renaming
print("\n After Renaming")
print(df)

Output Before Renaming:

course Mentor cost
C alex 1000
Python alice 2000
Java john 3000

Output After Renaming:

Subject Teacher cost
C alex 1000
Python alice 2000
Java john 3000

Related Article - Pandas DataFrame Column