Skip to content
Home » How to Rename Column in Pandas

How to Rename Column in Pandas

Introduction

Rename column in the panda’s data frame is used to change or modify the column names in the specified data frame. Sometimes the data frame or the dataset that is loaded may not contain the names of the columns. In such cases, renaming is a better choice to access the data easily and make the data frame more readable. There are several methods to rename the columns in pandas. Let’s look at them in detail in the below approaches.

Rename( ) in Pandas

rename() method in pandas data frame is used to modify the name of single or multiple columns in a dataframe. The column parameter in the rename() method also accepts a function or a dict type to modify the column names. The syntax and each of the parameters is explained below.

Syntax: data_frame.rename(mapper=Nonecolumns=None, index=Noneaxis=Nonecopy=True, errors=’ignore’  , level=None, inplace=False)

Parameters:
mapper : It can be dictionary type or a function that is used to map the specified column names with existing ones. This mapper can be target to axis parameter.

columns : It’s specifies the column names that is to be modified and also it can accept a function which is applied to each column in the dataframe. So it can either function or dict like object.

index : It acts just like columns parameter. Only change is that the column names can be modified using there index value instead of name of the column.

axis : The default values of axis is ‘index’ i.e., 0. It can be set to 1 or columns while modifying the column names using columns parameter.


copy : A Boolean parameter to copy the modified dataframe. Default set to True.


errors : { ‘ignore’ or ‘raise’ } When a dict-like mapper, index, or column has labels that aren’t present in the Index being changed, raise a KeyError. Existing keys will be renamed and extra keys will be ignored if ‘ignore’ is selected.


level : integer value. Used in renaming a multi index dataframe.


inplace : A Boolean value. It decides whether to return a copy of the dataframe or modify the existing one. Copy of dataframe is returned if inplace is set to False.

The above parameters can be used as per our requirement and the below examples makes you understand better.

Renaming a Single Column

The columns parameter in the rename() method is used to rename a column easily by specifying the old and the new value of the columns. The inplace = True in the below code modifies the existing dataframe instead of creating a copy of new dataframe.

# Importing pandas package
import pandas as pd

# Define a dictionary containing ICC rankings
students = {'name': ['Arun', 'Aakash', 'Emilia',
							'Newton', 'Auspin'],
			'marks': [90,56,87,59,87],
			'gender': ['male', 'male', 'female',
							'male', 'female']}

# Creating DataFrame with above dictionary
rank_df = pd.DataFrame(students)

# dataframe before renaming the columns
print('Dataframe before renaming columns :\n',rank_df)

#renaming a single columns
rank_df.rename(columns = {'marks':'Score'}, inplace = True)

# dataframe after renaming the columns
print('\nDataframe after renaming columns:\n',rank_df)

print("\nColumns in Dataframe after modifying :\n", rank_df.columns)

Output:

Dataframe before renaming columns :
      name  marks  gender
0    Arun     90    male
1  Aakash     56    male
2  Emilia     87  female
3  Newton     59    male
4  Auspin     87  female

Dataframe after renaming columns:
      name  Score  gender
0    Arun     90    male
1  Aakash     56    male
2  Emilia     87  female
3  Newton     59    male
4  Auspin     87  female

Columns in Dataframe after modifying :
 Index(['name', 'Score', 'gender'], dtype='object')

As we see in the above output the the column marks is changed to Score. It can also be done using the index value of the column with index parameter.

Renaming Multiple Columns

Renaming multiple columns in the pandas dataframe is siRenaming multiple columns in the pandas data frame is similar to renaming a single column. Passing the old and new column names that need to be modified is given in the columns parameter makes to change multiple column names at once.

# Importing pandas package
import pandas as pd

# Define a dictionary containing ICC rankings
students = {'name': ['Arun', 'Aakash', 'Emilia',
							'Newton', 'Auspin'],
			'marks': [90,56,87,59,87],
			'gender': ['male', 'male', 'female',
							'male', 'female']}

# Creating DataFrame with above dictionary
rank_df = pd.DataFrame(students)

# dataframe before renaming the columns
print('Dataframe before renaming columns :\n',rank_df)

#renaming multiple columns
rank_df.rename(columns = {'name':'Student','marks':'Score'}, inplace = True)

# dataframe after renaming the columns
print('\nDataframe after renaming columns:\n',rank_df)

print("\nColumns in Dataframe after modifying :\n", rank_df.columns)

Output:

Dataframe before renaming columns :
      name  marks  gender
0    Arun     90    male
1  Aakash     56    male
2  Emilia     87  female
3  Newton     59    male
4  Auspin     87  female

Dataframe after renaming columns:
   Student  Score  gender
0    Arun     90    male
1  Aakash     56    male
2  Emilia     87  female
3  Newton     59    male
4  Auspin     87  female

Columns in Dataframe after modifying :
 Index(['Student', 'Score', 'gender'], dtype='object')

As seen above multiple columns have been renamed. The column’s name and marks are changed to Student and Score. You can also modify all columns name in the similar way. But we got an easy approach below if you want to modify all the names.

Renaming All Columns Using list assignment

The List Assignment method can only be used if you want to modify all the column names in the dataframe. The columns are accessed using data_frame.columns and the list of new column names are assigned to it.

Note : Giving insufficient column names will throw an error message. Make sure that the list length is equal to the number of columns.

# dataframe before renaming the columns
print('Dataframe before renaming columns :\n',rank_df)

#renaming multiple columns
rank_df.columns = ['Student','Score','Gender']

# dataframe after renaming the columns
print('\nDataframe after renaming columns:\n',rank_df)

print("\nColumns in Dataframe after modifying :\n", rank_df.columns)

Output:

Dataframe before renaming columns :
      name  marks  gender
0    Arun     90    male
1  Aakash     56    male
2  Emilia     87  female
3  Newton     59    male
4  Auspin     87  female

Dataframe after renaming columns:
   Student  Score  Gender
0    Arun     90    male
1  Aakash     56    male
2  Emilia     87  female
3  Newton     59    male
4  Auspin     87  female

Columns in Dataframe after modifying :
 Index(['Student', 'Score', 'Gender'], dtype='object')

All the column names have been renamed with the new ones. The list assignment is an easy way if you are renaming all the columns names.

Columns parameter in rename() with function

As we seen in the syntax of rename() method, the column can also take a function which is applied to every column name in the dataframe. Given a scenario if you want to modify the column names slightly by removing to characters at the beginning of every column name. Then you can simply use an function inside a rename() method.

# Importing pandas package
import pandas as pd

# Define a dictionary containing ICC rankings
sample_dict = {'#$name': ['Arun', 'Aakash', 'Emilia',
							'Newton', 'Auspin'],
			        '#$marks': [90,56,87,59,87]
               }

# Creating DataFrame with above dictionary
rank_df = pd.DataFrame(sample_dict)

#printing columns
print(rank_df.columns)

#renaming the columns with a function
rank_df.rename(columns=lambda z: z[2:], inplace=True)

print(rank_df.columns)

Output:

Index([‘#$name’, ‘#$marks’], dtype=’object’)
Index([‘name’, ‘marks’], dtype=’object’)

Here the column name contains ‘#$’ special characters at the beginning of their names. So they are removed using a lambda function which takes each column name, slices it and replaces it with the old column name. The names #$name and #$marks are modified as name and marks in the above example.

Rename Columns Using data_frame.set_axis()

The name of the columns can also be modified using the set_axis() method. The specified column names renames according to the order of index values in the data frame.

# dataframe before renaming the columns
print('Dataframe before renaming columns :\n',rank_df)

#renaming all the columns
new_df = rank_df.set_axis(['X', 'Y', 'Z'], axis=1, inplace=False)

# dataframe after renaming the columns
print('\nDataframe after renaming columns:\n',new_df)

print("\nColumns in Dataframe after modifying :\n", new_df.columns)

Output:

Dataframe before renaming columns :
   Student  Score  Gender
0    Arun     90    male
1  Aakash     56    male
2  Emilia     87  female
3  Newton     59    male
4  Auspin     87  female

Dataframe after renaming columns:
         X   Y       Z
0    Arun  90    male
1  Aakash  56    male
2  Emilia  87  female
3  Newton  59    male
4  Auspin  87  female

Columns in Dataframe after modifying :
 Index(['X', 'Y', 'Z'], dtype='object')

The inplace=False in the above example returns a copy of the modified dataframe. If you want to modify the changes in the existing dataframe then set the inplace=True.

Conclusion

Here, we learnt about how to modify a single column, multiple columns and all the columns in the dataframe at once using list assignment. Also discussed about using the function under columns parameter. Used set_axis() method to modify the column names in the dataframe.