HOWTO · Pandas
How to Rename Columns in Pandas DataFrame
Learn how to rename every or selected column in a pandas DataFrame
On this page
Pandas offers three useful ways to rename columns in a DataFrame: assign a complete list through DataFrame.columns, map only the labels you want to change with DataFrame.rename(), or replace the full set with DataFrame.set_axis(). Choose based on whether you are changing every label or only a few.
Choose a column-renaming method
Use DataFrame.rename() for a small set of known old-to-new names. It keeps every unmapped label as it is, so it is convenient when only some columns need new names. Use DataFrame.columns or DataFrame.set_axis() when you have a replacement label for every column. Both full-list approaches require the new list to have the same number of labels as the DataFrame has columns.
Rename every column with DataFrame.columns
Assign a list directly to the columns attribute to replace all labels at once. The example starts with ['Name', 'Age', 'Marks'] and assigns a new label to each one. The original labels are shown here as selectable text; the printed result below contains only the renamed labels, ['Name', 'Age', 'Roll_no'].
Run this example:
import pandas as pd
scores = pd.DataFrame(
[["John", 20, 45], ["Peter", 21, 62], ["Scot", 25, 68]],
columns=["Name", "Age", "Marks"],
)
scores.columns = ["Name", "Age", "Roll_no"]
print(list(scores.columns))
['Name', 'Age', 'Roll_no']
This approach is direct, but you must spell out every label, including names that remain unchanged. That becomes cumbersome for a wide DataFrame. The replacement must also contain one name per existing column; otherwise pandas cannot assign the labels.
Rename selected columns with DataFrame.rename()
Pass a dictionary to columns: each key is an existing label, and its value is the replacement. The example changes only Marks and leaves the other labels alone. By default, DataFrame.rename() returns a new DataFrame and leaves the original unchanged, so keep the result:
Run this example:
import pandas as pd
scores = pd.DataFrame([["John", 20, 45]], columns=["Name", "Age", "Marks"])
renamed = scores.rename(columns={"Marks": "Score"})
print(list(renamed.columns))
['Name', 'Age', 'Score']
Alternatively, inplace=True applies the mapping to the existing DataFrame. In that mode, the call returns None, so do not assign its return value back to the DataFrame. A mapping is usually the clearest choice when only selected columns need renaming, and it avoids maintaining a list of all labels.
Replace the column labels with DataFrame.set_axis()
DataFrame.set_axis() is another way to replace the complete set of column labels. Pass the full label list and select the columns axis. Assign the returned DataFrame when using the default behavior:
Run this example:
import pandas as pd
scores = pd.DataFrame([["John", 20, 45]], columns=["Name", "Age", "Marks"])
renamed = scores.set_axis(["Name", "Age", "Score"], axis="columns")
print(list(renamed.columns))
['Name', 'Age', 'Score']
As with direct assignment, the list must cover every column. This method is useful when the new list is already available as a separate value or when you want to make the replacement part of a method chain. It does not provide the selective old-to-new mapping that DataFrame.rename() does.
Check the result and choose the right method
Inspect renamed.columns or print list(renamed.columns) after a change to confirm the labels you intend to use. This check is also useful before later code refers to a column by name: a misspelled or outdated label will not match the renamed column.
In short, use DataFrame.rename() for a few selected labels. Use DataFrame.columns for a straightforward full replacement, and DataFrame.set_axis() when the full list fits naturally into a method call or chain. For either full-list method, count the labels and keep their order aligned with the DataFrame’s columns.