Skip to content
Home » Rename Column name in Pandas

Rename Column name in Pandas

The pandas data frame’s rename() method is used to change the name of a single or multiple columns. To modify the column names, the column parameter in the rename() method also accepts a function or a dict type.

Renaming a Single column

The columns argument in the rename() method is used to simply rename a column by providing the old and new column values. The inplace = True in the following code updates the existing dataframe rather than creating a new dataframe copy.

#renaming a single column in dataframe
df.rename(columns = {old_column_name:new_column_name}, inplace = True)

Example:

df.rename(columns = {'name':'Student'}, inplace = True)

Renaming Multiple Columns

It is simple to rename several columns in a pandas dataframe. Changing the name of multiple columns is comparable to changing the name of a single column. By passing the old and new column names in the columns argument, several column names can be changed at the same time.

#renaming multiple columns
df.rename(columns = {'old_col_name':'new_col_name','old_col_name':'new_col_name'}, inplace = True)

Renaming All Columns

The List Assignment method can only be used if you want to change the names of all the columns in the dataframe. Data frame.columns is used to access the columns, and a list of new column names is assigned to it.

#df.columns = list_of_all_columns
df.columns = ['Name','phone','marks']