How to Rename Column Names in a Pandas DataFrame: A Python Guide

A Pandas DataFrame is a popularly used data manipulation/analysis library that contains relevant information. It makes the data more readable and accessible to the users. A Pandas DataFrame assists in handling a large amount of data. The process of making the data more suitable and visually appealing to the users is called Data cleaning which is an essential part of Data Science. Renaming the column names is one part of data cleaning. 

This article discusses the significance of renaming the column names along with the methods and their relevant examples.

How to Rename Column Names in a Pandas DataFrame?

A Python DataFrame column can be renamed using the methods like “rename()”, “set_axis()”, “columns.str.replace()”, “add_suffix”, “add_prefix”, and using a new list of columns names. The columns in the DataFrame hold the respective data for a certain variable. The Pandas DataFrame contains the labeled axis. A column in the DataFrame must contain the same type of data while the rows can have varying data. 

A column can be renamed in a Pandas DataFrame in the following ways:

  1. Rename Pandas DataFrame Column Using rename() Function 
  2. Rename Pandas DataFrame Column Using the set_axis() Function 
  3. Rename Pandas DataFrame Column Using columns.str.replace()
  4. Rename Pandas DataFrame Column by Using a List
  5. Rename Pandas DataFrame Column Using add_prefix() and add_suffix() Function 

Let us discuss each method in detail.

Method 1: Rename Pandas DataFrame Column Using rename() Function 

One way to rename the columns is to use the rename() function which is particularly helpful when changing the names for specified columns. 

Let us discuss different examples of changing the column names by executing/utilizing the rename() function.

Example 1: Using rename() Method to Rename a Single Column 

The rename() method is the simplest way to rename the column. The rename method takes the first parameter as a dictionary object. The keys of the dictionary object represent the previous column names while the values represent the updated column names. A single column can be renamed in the Pandas DataFrame using the rename() method in the following way:

import pandas as pd
astro_objects = {'Planets': ['Mars', 'Saturn', 'Uranus',
'Earth', 'Neptune'],
'stars': ['sun', 'Proxima', 'Sirius',
'Betelgeuse', 'Pollux'],
'galaxies': ['Andromeda', 'black eye', 'Milkyway',
'cartwheel', 'pinkwheel']}
astro_objects_pd = pd.DataFrame(astro_objects)
print(astro_objects_pd)
astro_objects_pd.rename(columns = {'Planets':'PLANETS'}, inplace = True)
print("\n The names after the modification of first column: \n",astro_objects_pd.columns)

In the above code,

  • The Pandas library is imported as pd.
  • The dictionary astro_objects is defined with three columns as keys representing different astronomical objects and their respective values.
  • The DataFrame is created using pd.DataFrame and saved in the variable astro_objects_pd.
  • The data of the newly created DataFrame is printed.
  • The rename() function is called on the DataFrame with two parameters: the “columns = {‘Planets’:’PLANETS’}”  and the value of inplace as true. As a result, the ‘Planets’ will be renamed to ‘PLANETS’.
  • The names of the columns of the modified DataFrame are printed.

Output

The following output displays how the column name Planets has been changed to PLANETS in the DataFrame astro_objects_pd using the rename() method in Python:

Example 2: Using rename() to Rename Two or More Columns

The rename() method can be used to rename multiple columns of a Pandas DataFrame. The dictionary parameter will have more than one key-value pair to rename the columns. Multiple columns can be renamed in the Pandas DataFrame in the following way:

import pandas as pd
astro_objects = {'Planets': ['Mars', 'Saturn', 'Uranus',
'Earth', 'Neptune'],
'stars': ['sun', 'Proxima', 'Sirius',
'Betelgeuse', 'Pollux'],
'galaxies': ['Andromeda', 'black eye', 'Milkyway',
'cartwheel', 'pinkwheel']}
astro_objects_pd = pd.DataFrame(astro_objects)
print(astro_objects_pd)
astro_objects_pd.rename(columns = {'Planets':'PLANETS', 'stars':'STARS', 'galaxies':'GALAXIES'}, inplace = True)
print("\n The names after the modification each column are: \n",astro_objects_pd.columns)

In the above code,

  • The same Pandas DataFrame astro_objects_pd is created.
  • The rename() function is called on multiple columns instead of a single one and all the columns’ names have been modified.
  • The modified DataFrame is printed.

Output

The column names Planets, stars, and galaxies have been changed to PLANETS, STARS, and GALAXIES. The following output displays how the names of multiple columns of a Pandas DataFrame can be changed using the rename() method:

Method 2: Rename Pandas DataFrame Column Using the set_axis() Function

The set_axis() method enables the user to define the index value of an axis. The function set_axis() can also be used to rename the column names where the axis will be passed as the value of columns. This will change the names of selected columns. The following code shows how the column names can be renamed using the set_axis() method:

import pandas as pd
astro_objects = {'Planets': ['Mars', 'Saturn', 'Uranus',
'Earth', 'Neptune'],
'stars': ['sun', 'Proxima', 'Sirius',
'Betelgeuse', 'Pollux'],
'galaxies': ['Andromeda', 'black eye', 'Milkyway',
'cartwheel', 'pinkwheel']}
astro_objects_pd = pd.DataFrame(astro_objects)
print(astro_objects_pd)
astro_objects_pd.set_axis(['PLANETS', 'STARS', 'GALAXIES'], axis='columns', inplace=True)
print("\n The names after the modification each column are: \n",astro_objects_pd)

In this code,

  • The set_axis() function is called on the DataFrame astro_objects_pd with the parameter values of a list containing the modified names of the columns, the axis value set to columns, and the inplace value to be True.
  • The modified DataFrame is printed.

Output

The original names of the columns have been changed to their uppercase equivalents. The output displays the Pandas DataFrame with renamed columns:

Method 3: Rename Pandas DataFrame Column Using columns.str.replace()

The replace method can be used to rename the columns as well. The columns.str.replace method is called on the pandas dataframe which in our case is astro_objects_pd and in the parameters, the first parameter provided is the previous name of the column and the second parameter is the updated column name. The following code explains how columns in a DataFrame can be renamed by calling columns.str.replace() method on the pandas DataFrame:

import pandas as pd
astro_objects = {'Planets': ['Mars', 'Saturn', 'Uranus',
'Earth', 'Neptune'],
'stars': ['sun', 'Proxima', 'Sirius',
'Betelgeuse', 'Pollux'],
'galaxies': ['Andromeda', 'black eye', 'Milkyway',
'cartwheel', 'pinkwheel']}
astro_objects_pd = pd.DataFrame(astro_objects)

astro_objects_pd = pd.DataFrame(astro_objects)
print("The original DataFrame is: \n ",astro_objects_pd)
print("\n ")
astro_objects_pd.columns = astro_objects_pd.columns.str.replace('Planets', 'col_planets')
astro_objects_pd.columns = astro_objects_pd.columns.str.replace('stars', 'col_stars')
astro_objects_pd.columns = astro_objects_pd.columns.str.replace('galaxies', 'col_galaxies')
print("The modified DataFrame is: \n ",astro_objects_pd)

In the above code,

  • columns.str.replace is called on the columns of the astro_objects_pd DataFrame to rename its columns.
  • The DataFrame with the modified column names will be then printed.

Output

The names of the columns Planets, stars, and galaxies have been changed to the columns col_planets, col_stars, and col_galaxies respectively. The output displays the modified DataFrame with the column names renamed using columns.str.replace function:

Method 4: Rename Pandas DataFrame Column by Using a List

A list with the updated/modified column names can be assigned to the columns attribute of the DataFrame object. Doing so will rename/update all the columns of the selected DataFrame. This method is not useful when a few column names need to be updated since this method updates all the column names. 

The following code explains how the column names can be updated using a list containing the updated names of the columns:

import pandas as pd
astro_objects = {'Planets': ['Mars', 'Saturn', 'Uranus',
'Earth', 'Neptune'],
'stars': ['sun', 'Proxima', 'Sirius',
'Betelgeuse', 'Pollux'],
'galaxies': ['Andromeda', 'black eye', 'Milkyway',
'cartwheel', 'pinkwheel']}
astro_objects_pd = pd.DataFrame(astro_objects)
print("The original DataFrame is: \n ",astro_objects_pd)
print("\n ")
astro_objects_pd.columns = ['P', 'S', 'G']
print("The modified DataFrame is: \n ",astro_objects_pd)

In the above code,

  • A new list of column names with the values P, S, and G is assigned to the DataFrame astro_objects_pd.
  • The DataFrame with the renamed column names is printed.

Output

The following output displays how the column names of a Pandas DataFrame can be modified by assigning a new list of column names:

Method 5: Rename Pandas DataFrame Column Using add_prefix() and add_suffix() 

The asdd_prefix() and add_suffix functions can add a starting and ending string value to the column name. Here is a code that uses the add_prefix and add_suffix method to rename the columns of the pandas dataframe:

import pandas as pd
astro_objects = {'Planets': ['Mars', 'Saturn', 'Uranus',
'Earth', 'Neptune'],
'stars': ['sun', 'Proxima', 'Sirius',
'Betelgeuse', 'Pollux'],
'galaxies': ['Andromeda', 'black eye', 'Milkyway',
'cartwheel', 'pinkwheel']}
astro_objects_pd = pd.DataFrame(astro_objects)
print("The original DataFrame is: \n ",astro_objects_pd)
print("\n ")
astro_objects_pd = astro_objects_pd.add_prefix('Astro_')
astro_objects_pd = astro_objects_pd.add_suffix('_Examples')
print("The modified DataFrame is: \n ",astro_objects_pd)

In the above code,

  • The Panda DataFrame with different celestial bodies is printed.
  • The method add_prefix is called on the DataFrame astro_objects_pd with a parameter value of “Astro_”. This will add a prefix of “Astro_” to each column name of the selected DataFrame.
  • Similarly, add_suffix is called on astro_objects_pd with the parameter value of _Examples. This will add the suffix “_Examples” to the column names of the given DataFrame.
  • The modified DataFrame with the renamed column names is printed.

Output

A prefix Astro_ and a suffix _Examples are added to each column name in  DataFrame astro_objects_pd. The following output displays how prefixes and suffixes can be added to the column names in the Pandas DataFrame:

This way you can rename the dataframe’s columns to perform specific tasks such as convenient interpretation of data, smooth sorting, etc.

Conclusion

A Python DataFrame column can be renamed using the methods like “rename()”, “set_axis()”, “columns.str.replace()”, “add_suffix”, “add_prefix”, and using a new list of columns names. Renaming the column’s name in the data makes the data more clear and visually appealing according to what the user wants. This article has discussed different methods to rename the column names in a Pandas DataFrame and explains each method with a suitable example.