Rename multiple columns in Pandas

  • Post category:Pandas

In this Pandas tutorial, you will learn how to rename multiple columns in Pandas. You can easily rename multiple columns of Pandas DataFrame with the following code.

# import Pandas library
import pandas as pd

# create a dataframe
var = pd.DataFrame({'A': [5, 10, 15, 20], 'B': [10, 20, 30, 40], 'C': [50, 60, 70, 80]})

# rename B to S and C to X
var.rename(columns={'B': 'S', 'C': 'X'}, inplace=True)

# display the dataframe
print(var)

Output:

    A   S   X
0   5  10  50
1  10  20  60
2  15  30  70
3  20  40  80

Free resources to learn advanced skills: AiHints and CodeAllow