rename() (Pandas)
df.rename() changes column or index labels by mapping old names to new ones with a dict or a function. It is the clean, non-destructive way to relabel without rebuilding the frame.
Pandas
Pass columns={'old': 'new'} to relabel columns or index={...} to relabel rows; only the keys you mention change, everything else stays. You can also pass a function to mapper to transform every label, like str.lower to lowercase all column names at once.
rename returns a new frame by default, so reassign the result (or use inplace=True). To replace the labels wholesale rather than selectively, assign directly to df.columns with a full list instead.
Use axis='columns' or axis='index' with the mapper form for a terser call, and add_prefix/add_suffix for the common case of tagging every column name.
Syntax
df.rename(columns={...}, index={...})Examples
Python
import pandas as pd
df = pd.DataFrame({"a": [1]})
print(df.rename(columns={"a": "x"}))Output:
x
0 1Learn it in a lesson
- Renaming →