How to Rename Column(s) in Pandas DataFrame?

Rename Columns Pandas DataFrame

You can rename a single column or multiple columns of a pandas DataFrame using pandas.DataFrame.rename() method.

In the following set of examples, we will learn how to rename a single column, and how to rename multiple columns of Pandas DataFrame.

Example 1: Rename Single Column

To rename a single column, you can use DataFrame.rename() function as shown below.

Python Program

import pandas as pd

#initialize a dataframe
df = pd.DataFrame(
	[[41, 72, 67, 91],
	[21, 78, 69, 87],
	[95, 74, 56, 88],
	[54, 54, 76, 78]],
	columns=['a', 'b', 'c', 'd'])	

#rename single column
df.rename(columns={'b': 'k'}, inplace=True)

#print the columns
print(df)
Run this program ONLINE

Output

    a   k   c   d
0  41  72  67  91
1  21  78  69  87
2  95  74  56  88
3  54  54  76  78

The column name b has been renamed to k for the Pandas DataFrame.

Example 2: Rename Multiple Columns

To rename multiple columns, you can use DataFrame.rename() method with multiple old column names and new column names as key:value pairs as shown below.

Python Program

import pandas as pd

#initialize a dataframe
df = pd.DataFrame(
	[[41, 72, 67, 91],
	[21, 78, 69, 87],
	[95, 74, 56, 88],
	[54, 54, 76, 78]],
	columns=['a', 'b', 'c', 'd'])	

#rename single column
df.rename(columns={'b': 'k', 'c': 'm'}, inplace=True)

#print the columns
print(df)
Run this program ONLINE

Output

    a   k   m   d
0  41  72  67  91
1  21  78  69  87
2  95  74  56  88
3  54  54  76  78

The column named b has been renamed to k and column c has been renamed to m.

Summary

In this Pandas Tutorial, we renamed one or more columns in-place, using pandas.DataFrame.rename() method, with the help of well detailed Python example programs.