Use DataFrame.rename(columns={...}) to change selected column names in pandas. Assign its returned DataFrame to keep the change, or choose a function to transform every label. If you are replacing the entire set of names, use set_axis or assign to df.columns instead.
Rename one or more selected columns
Pass a dictionary mapping existing labels to their new names with the columns keyword:
As an Amazon Associate I earn from qualifying purchases.
df = df.rename(columns={"old_name": "new_name"})
To rename several columns at once, add each old-to-new pair to the same mapping:
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
df = df.rename(columns={
"first": "first_name",
"last": "last_name",
})
By default, labels not included in the mapping stay unchanged, and mapping keys that do not match a column are ignored. To make a missing requested label an error instead, use errors="raise":
#1 Best Overall
df = df.rename(columns={"old_name": "new_name"}, errors="raise")
The mapping or function used to rename labels must produce one-to-one results, as required by the pandas DataFrame.rename API.
Keep the renamed DataFrame
rename returns a DataFrame by default; it does not change the variable df automatically. Assign the result back to df, as in the examples above, or save it under a different variable name.
Rank #2
You can also use inplace=True to modify the existing object:
df.rename(columns={"old_name": "new_name"}, inplace=True)
In that mode, the method returns None, so do not write df = df.rename(..., inplace=True); doing so replaces df with None. Prefer the explicit columns= keyword over the more general mapper, axis= form: pandas recommends keyword arguments to make the intent clear.
Transform every column name with a function
Pass a function to columns when the same transformation should apply to every label. For example, this lowercases all column names:
df = df.rename(columns=str.lower)
The function form is useful for consistent label changes across a DataFrame; use a mapping when only particular names need to change.
Replace the complete list of column names
If you know every new label and want to replace the full set, use set_axis or assign a list directly to df.columns. Unlike a partial mapping, the replacement list must provide a label for every existing column.
Recommended Free Tools
df = df.set_axis(["date", "city", "sales"], axis="columns")
# Or replace the labels directly:
df.columns = ["date", "city", "sales"]
axis="columns" identifies the column axis. The number of supplied labels must match the number of DataFrame columns. See the pandas DataFrame.set_axis API for the method’s accepted inputs.
Best Value
Do not confuse column labels with axis names
A DataFrame’s columns are an Index. rename_axis(columns=...) changes the name attached to that Index, or the names of levels in a MultiIndex; it does not change ordinary labels such as "sales" or "date". Use rename(columns=...) to change those labels. For MultiIndex columns, rename also provides level to target a particular label level. The distinction is documented in the pandas DataFrame.rename_axis API.
assign is different again: it creates columns while keeping existing ones, and overwrites a column if the target name already exists. Adding a new named column with assign does not, by itself, remove or rename the old column. See the pandas DataFrame.assign API.
Version note for pandas 3.0 and older
The current stable documentation references identify pandas 3.0.5 for rename and assign, and pandas 3.0.6 for set_axis. In pandas 3.0, the copy argument to rename is ignored and deprecated for removal in pandas 4.0. The method always returns a new object and uses lazy copying with Copy-on-Write, so do not set copy to control copying in pandas 3.0. The pandas 2.1 rename reference describes different copy behavior; check the documentation for the version installed in your environment if you rely on older pandas.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




