How to Rename Columns in a Pandas DataFrame

01.16.2021

Intro

Often reading data, especially from csvs, the columns are not in a readable format or have much meanings. In these cases you may want to rename the column. In this article, we will see how to rename columns in a Pandas DataFrame.

Renaming Columns

To rename columns in a pandas DataFrame we can use the rename method. We pass a named parameter columns with a dictionary of the old column names mapped to the new column names. In the example below, we rename the "person" and "lastName" columns.

import pandas as pd

df = pd.DataFrame([
	{
		"person": "James",
		"sales": 1000,
		"lastName": "Taylor",
	},
	{
		"person": "Clara",
		"sales": 3000,
		"lastName": "Brown"
	}
])

newDf = df.rename(
 columns={
	'pearson' : 'FirstName', 
	'lastName' : 'LastName'
 }
)
print(newdf.head())