Open In App

How to rename columns in Pandas DataFrame

Last Updated : 12 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, we will see how to rename column in Pandas DataFrame. The simplest way to rename columns in a Pandas DataFrame is to use the rename() function. This method allows renaming specific columns by passing a dictionary, where keys are the old column names and values are the new column names.

Python
import pandas as pd

# Sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})

# Renaming columns
df.rename(columns={'A': 'X', 'B': 'Y', 'C': 'Z'}, inplace=True)
print(df)

Output:

   X  Y  Z
0 1 4 7
1 2 5 8
2 3 6 9

This code changes column names from ‘A’ to ‘X’ , ‘B’ to ‘Y’ and ‘C’ to ‘Z’.

renaming-columns-in-pandas

The rename method is straightforward and flexible, but there are other useful methods, which we’ll explore below.

Rename Columns by Assigning a List of New Column Names

If you need to rename all columns at once, you can directly assign a list of new column names to the columns attribute of the DataFrame. This approach is concise and convenient for changing multiple column names simultaneously.

Python
# Sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})

# Rename all columns
df.columns = ['X', 'Y', 'Z']
df

Output:

methods-2-dataframe

This replaces the original column names with the new list [‘X’, ‘Y’, ‘Z’]. Make sure the length of the list matches the number of columns in the DataFrame.

Rename Columns Using set_axis

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.

Python
# Sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

# Rename columns using set_axis
df = df.set_axis(['Alpha', 'Beta'], axis=1)
df

Output:

method-3

Here, set_axis renames the columns to [‘Alpha’, ‘Beta’]. Setting inplace=False ensures the operation returns a new DataFrame, leaving the original unchanged.

Add Prefix or Suffix to Rename Column Names

To systematically rename columns by adding a prefix or suffix, you can use the add_prefix or add_suffix methods. This is helpful for categorizing columns or distinguishing them from other sets of data.

Python
# Sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

# Add a prefix to all columns
df_prefixed = df.add_prefix('col_')
print("Prefixed DataFrame:\n", df_prefixed)

# Add a suffix to all columns
df_suffixed = df.add_suffix('_data')
print("Suffixed DataFrame:\n", df_suffixed)

Output:

Prefixed DataFrame:
col_A col_B
0 1 4
1 2 5
2 3 6

Suffixed DataFrame:
A_data B_data
0 1 4
1 2 5
2 3 6

The add_prefix method renames columns by adding ‘col_’ as a prefix, while add_suffix appends ‘_data’ as a suffix.

Replace a Character in Column Names

If you need to modify column names by replacing specific characters, you can use the str.replace function on the columns attribute. This method is handy for removing or altering unwanted characters or spaces.

Python
# Sample DataFrame with spaces in column names
df = pd.DataFrame({'First Name': ['A', 'B', 'C'], 'Last Name': ['D', 'E', 'F']})

# Replace spaces with underscores
df.columns = df.columns.str.replace(' ', '_')
print(df)

Output:

  First_Name Last_Name
0 A D
1 B E
2 C F

This replaces spaces in column names with underscores, which is helpful for maintaining consistent naming conventions.

Renaming Columns in Pandas DataFrame – FAQs

How do I rename multiple columns in pandas?

You can rename multiple columns in a Pandas DataFrame using a dictionary with old column names as keys and new column names as values passed to the rename() method.

import pandas as pd
# Example DataFrame
df = pd.DataFrame({
‘A’: [1, 2, 3],
‘B’: [4, 5, 6],
‘C’: [7, 8, 9]
})
# Rename multiple columns
df.rename(columns={‘A’: ‘New_A’, ‘B’: ‘New_B’}, inplace=True)
print(df)

How do I rename only certain columns in pandas?

To rename multiple columns, use the rename() method, passing a dictionary where keys are old column names and values are new names.

For example:

import pandas as pd
df = pd.DataFrame({ 'A': [1, 2, 3], 'B': [4,5,6], 'C' : [7,8,9]})
# Rename Column
df = df.rename(columns={'A': 'Alpha', 'B': 'Beta', 'C': 'Gamma'})

How to rename column names Pandas with list?

If you want to rename only specific columns, you can include only the columns you want to rename in the dictionary inside rename().

# Rename only specific columns
df = df.rename(columns={'A': 'Alpha', 'B': 'Beta'}) # 'C' will remain unchanged

How do you set column names in DataFrame?

If you want to rename all columns at once with a list, assign a list of new names directly to df.columns.

# Rename all columns using a list
df.columns = ['Alpha', 'Beta', 'Gamma']


Next Article

Similar Reads

three90RightbarBannerImg