How to rename column name in pandas

The Pandas DataFrame rename() method can be used to rename one or more columns at once:

# rename the 'Salary' column to 'Income' df = df.rename(columns={'Salary': 'Income'})

Pandas is a popular data manipulation library in Python, used for data analysis, cleaning, and manipulation tasks. One common task is to rename columns in a Pandas DataFrame. There are several methods to rename columns in Pandas, including the rename() method, the columns attribute, and the set_axis() method. Let's explore each method in detail:

Method 1: Using the rename() method

The rename() method is the most commonly used method to rename columns in a Pandas DataFrame. The rename() method can be used to rename one or more columns at once, by passing a dictionary of old column names to new column names as the argument.

import pandas as pd # create a sample dataframe data = {'Name': ['John', 'Doe', 'Bill','Jim','Harry','Ben'], 'TotalMarks': [82, 38, 63,22,55,40], 'Grade': ['A', 'E', 'B','E','C','D'], 'Promoted': [True, False,True,False,True,True]} df = pd.DataFrame(data) # rename the 'Grade' column to 'Level' df = df.rename(columns={'Grade': 'Level'}) print(df.head())
#Output: Name TotalMarks Level Promoted 0 John 82 A True 1 Doe 38 E False 2 Bill 63 B True 3 Jim 22 E False 4 Harry 55 C True

In the above example, we used the rename() method to rename the 'Grade' column to 'Level'. The old column name 'Grade' is passed as a key, and the new column name 'Level' is passed as a value in a dictionary.

Method 2: Using the columns attribute

The columns attribute is another way to rename columns in a Pandas DataFrame. It allows you to directly assign a list of new column names to the columns attribute of the DataFrame.

import pandas as pd # create a sample dataframe data = {'Name': ['John', 'Doe', 'Bill','Jim','Harry','Ben'], 'TotalMarks': [82, 38, 63,22,55,40], 'Grade': ['A', 'E', 'B','E','C','D'], 'Promoted': [True, False,True,False,True,True]} df = pd.DataFrame(data) # rename the 'Grade' column to 'Level' and 'Promoted' column to 'Passed' df.columns = ['Name', 'TotalMarks', 'Level','Passed'] print(df.head())
#Output: Name TotalMarks Level Passed 0 John 82 A True 1 Doe 38 E False 2 Bill 63 B True 3 Jim 22 E False 4 Harry 55 C True

In the above example, used the columns attribute to assign a list of new column names to the columns attribute of the DataFrame.

Method 3: Using the set_axis() method

The set_axis() method is a lesser-known method to rename columns in a Pandas DataFrame. It allows you to rename both rows and columns of a DataFrame at once.

import pandas as pd # create a sample dataframe data = {'Name': ['John', 'Doe', 'Bill','Jim','Harry','Ben'], 'TotalMarks': [82, 38, 63,22,55,40], 'Grade': ['A', 'E', 'B','E','C','D'], 'Promoted': [True, False,True,False,True,True]} df = pd.DataFrame(data) # rename the 'Grade' column to 'Level' and 'Promoted' column to 'Passed' df = df.set_axis(['Name', 'TotalMarks', 'Level','Passed'], axis=1, copy=False) print(df.head())
#Output: Name TotalMarks Level Passed 0 John 82 A True 1 Doe 38 E False 2 Bill 63 B True 3 Jim 22 E False 4 Harry 55 C True

Method 4: using str.replace() function

To rename multiple columns in Pandas using the DataFrame.columns.str.replace function, you can use regular expressions to match the old column names and replace them with the new column names.

import pandas as pd # create a sample dataframe data = {'Name': ['John', 'Doe', 'Bill','Jim','Harry','Ben'], 'TotalMarks': [82, 38, 63,22,55,40], 'Grade': ['A', 'E', 'B','E','C','D'], 'Promoted': [True, False,True,False,True,True]} df = pd.DataFrame(data) # rename the 'Grade' column to 'Level' and 'Promoted' column to 'Passed' df.columns = df.columns.str.replace('Grade', 'Level').str.replace('Promoted', 'Passed') print(df.head())
# Output: Name TotalMarks Level Passed 0 John 82 A True 1 Doe 38 E False 2 Bill 63 B True 3 Jim 22 E False 4 Harry 55 C True

In the above example used the DataFrame.columns.str.replace function to rename the 'Grade' column to 'Level' and 'Promoted' column to 'Passed'. Here, used two separate str.replace methods, one for each old column name we want to replace. The str.replace method updates the column names of the DataFrame in place and returns the updated DataFrame.

Rename a specific character in all column pandas

To rename a specific character in all columns of a Pandas DataFrame, you can use the str.replace() method of the DataFrame columns.

import pandas as pd # create a sample dataframe data = {'School_Name': ['John', 'Doe', 'Bill','Jim','Harry','Ben'], 'School_TotalMarks': [82, 38, 63,22,55,40], 'School_Grade': ['A', 'E', 'B','E','C','D'], 'School_Promoted': [True, False,True,False,True,True]} df = pd.DataFrame(data) # replace 'School_' in all column names with 'Class_' df.columns = df.columns.str.replace('School_', 'Class_') print(df.head())
#Output: Class_Name Class_TotalMarks Class_Grade Class_Promoted 0 John 82 A True 1 Doe 38 E False 2 Bill 63 B True 3 Jim 22 E False 4 Harry 55 C True

In the above example, used the str.replace() method to replace the 'School_' character in all column names with 'Class_'. So, accessed the columns of the DataFrame using the .columns attribute and then applied the str.replace() method to each column name. The updated column names are then assigned back to the columns attribute of the DataFrame.

Conclusion

To rename a column in a Pandas DataFrame, you can use the rename() method. You need to provide a dictionary with the current column name as the key and the new column name as the value. This method will update the DataFrame with the new column names, and the original DataFrame will be modified in place.