Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

How to Rename Columns in Pandas

Use pandas rename(columns=...) for selected labels, a function to transform all names, or set_axis when replacing the complete column list.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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":

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.

You can also use inplace=True to modify the existing object:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Leave a Reply

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.