Rename Columns in Pandas DataFrame
Renaming columns in a Pandas DataFrame allows you to change column names. For example, a DataFrame with columns ['A', 'B', 'C'] and you want to rename them to ['X', 'Y', 'Z']; after renaming, DataFrame will have the new column names ['X', 'Y', 'Z'].

Let's explore different methods to rename columns in a Pandas DataFrame.
Using rename() Function
The rename() function allows renaming specific columns by passing a dictionary, where keys are the old column names and values are the new column names.
Example: Here we rename only columns 'A' and 'B' in a DataFrame.
import pandas as pd
df = pd.DataFrame({'A': [10, 20], 'B': [30, 40], 'C': [50, 60]})
df.rename(columns={'A': 'X', 'B': 'Y'}, inplace=True)
print(df)
Output
X Y C 0 10 30 50 1 20 40 60
Explanation:
- df.rename(columns={...}, inplace=True) renames specified columns in the dictionary.
- Only 'A' -> 'X' and 'B' -> 'Y' are renamed; 'C' remains unchanged.
- inplace=True updates df directly.
Rename Columns by Assigning a List
To rename all columns at once, you can assign a new list to df.columns. This is concise and useful when changing multiple column names simultaneously.
Example: This example renames all columns to ['X', 'Y', 'Z'].
import pandas as pd
df = pd.DataFrame({'A': [5, 6], 'B': [7, 8], 'C': [9, 10]})
df.columns = ['X', 'Y', 'Z']
print(df)
Output
X Y Z 0 5 7 9 1 6 8 10
Explanation:
- Assign a new list of names to df.columns.
- Ensure the list length matches the number of columns.
- Print df to verify the updated column names.
Using set_axis() Method
The set_axis method can be used to rename all columns in a DataFrame. This function takes a list of new column names and an axis (0 for rows, 1 for columns) and returns a DataFrame with renamed columns.
Example: This example renames columns to ['Alpha', 'Beta', 'Gamma'].
import pandas as pd
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4], 'C': [5, 6]})
df = df.set_axis(['Alpha', 'Beta', 'Gamma'], axis=1)
print(df)
Output
Alpha Beta Gamma 0 1 3 5 1 2 4 6
Explanation:
- df.set_axis([...], axis=1) renames all columns.
- Returns a new DataFrame.
- Print df to see the renamed columns.
Adding Prefix or Suffix
add_prefix() or add_suffix() adds a prefix or suffix to all column names. Useful for distinguishing columns from multiple datasets or categories.
Example: This example adds 'new_' as prefix and '_val' as suffix.
import pandas as pd
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
pre = df.add_prefix('new_')
suf = df.add_suffix('_val')
print("Prefixed:\n", pre)
print("Suffixed:\n", suf)
Output
Prefixed:
new_A new_B
0 1 3
1 2 4
Suffixed:
A_val B_val
0 1 3
1 2 4
Explanation:
- add_prefix('new_') prepends 'new_' to each column name.
- add_suffix('_val') appends '_val' to each column name.
- Returns new DataFrames with updated column names.
Replace a Character in Column Names
Use df.columns.str.replace() to replace unwanted characters in column names. Handy for spaces, symbols or standardizing column names.
Example: This example replaces spaces with underscores.
import pandas as pd
df = pd.DataFrame({'First Name': ['A','B'], 'Last Name': ['C','D']})
df.columns = df.columns.str.replace(' ', '_')
print(df)
Output
First_Name Last_Name 0 A C 1 B D
Explanation:
- df.columns.str.replace(' ', '_') replaces spaces with underscores.
- Assign the result back to df.columns.
- Print df to confirm the updated names.