Rename a column in Pandas

  • Post category:Pandas

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

# import Pandas library
import pandas as pd

# create a dataframe
var = pd.DataFrame({'A': [41, 42, 43, 44], 'B': [46, 47, 48, 49], 'C': [51, 52, 53, 54]})

# rename column B to M
var.rename(columns={'B': 'M'}, inplace=True)

# display the dataframe
print(var)

Output:

    A   M   C
0  41  46  51
1  42  47  52
2  43  48  53
3  44  49  54

Free resources to learn advanced skills: AiHints and CodeAllow