The Easiest Way to Rename a Column in Pandas

2021-01-12

Given a DataFrame df with columns ["date", "region", "revenue"], you can rename the “revenue” column to “sales” by passing a mapping to the .rename() method:

import pandas as pd

df = pd.read_csv("https://jbencook.s3.amazonaws.com/data/dummy-sales.csv")

df.rename(columns={"revenue": "sales"}, inplace=True)
df.columns

# Expected result
# Index(['date', 'region', 'sales'], dtype='object')

This is the easiest way to rename a single column, but you can rename multiple columns by adding more <current_name>: <new_name> pairs to your dictionary.

Or you can use a mapping function:

df.rename(columns=str.upper, inplace=True)
df.columns

# Expected result
# Index(['DATE', 'REGION', 'SALES'], dtype='object')

Here we pass the classmethod str.upper(), which is equivalent to passing in a lambda function: lambda s: s.upper().

If you want to rename all your columns with a list, you can overwrite the df.columns attribute:

df.columns = ["_date", "_region", "_revenue"]
df.columns

# Expected result
# Index(['_date', '_region', '_revenue'], dtype='object')

But this approach does not work for renaming a single column by index:

# Don't do this!
df.columns[0] = "DATE"

# Raises TypeError

Here’s a Jupyter notebook if you want to try it for yourself. And if you like cool Pandas tricks, check out my post on pd.melt().

Leave a Comment

Your email address will not be published. Required fields are marked *