You are currently viewing Rename Columns in Pandas: A Beginner’s Guide to 4 Powerful Methods
Rename Columns in Pandas: 4 Methods

Rename Columns in Pandas: A Beginner’s Guide to 4 Powerful Methods

To rename columns in Pandas is a common data-wrangling task that every data analyst or data scientist encounters at some point. Whether you need to standardize the column names for consistency, fix typos or formatting issues, or make them more descriptive and easier to understand, renaming columns can help you achieve these goals.

This article explores four ways to rename Pandas columns. These methods are essential for manipulating and transforming your data, and knowing how to use them efficiently can save you time and effort in your data analysis projects.

Irrespective of your level of experience as a data analyst, this guide will help you understand the different techniques for renaming columns in Pandas and choose the best method for your specific needs. Let’s get started!

Method 1: Using the rename() method

The rename() method is a built-in function in Pandas that allows you to rename one or more columns of a DataFrame. This method is quite flexible and will enable you to specify the new column names using various methods, such as passing a dictionary or a function.

Step-by-step instructions for using the rename() method to rename columns in Pandas:

First, create a Pandas DataFrame that contains the columns you want to rename. For example, let’s say we have the following DataFrame:

import pandas as pd

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

Output:

   A  B
0  1  4
1  2  5
2  3  6

To rename columns using the rename() method, pass a dictionary that maps the old column names to the new ones as the columns parameter. For example, let’s rename the ‘A’ and ‘B’ columns to ‘X’ and ‘Y’, respectively:

df = df.rename(columns={'A': 'X', 'B': 'Y'})
print(df)

Output:

   X  Y
0  1  4
1  2  5
2  3  6

As you can see, the ‘A’ and ‘B’ columns have been renamed to ‘X’ and ‘Y’, respectively.

You can also use a function to rename columns based on specific criteria. For example, let’s say we want to rename all columns to uppercase:

df = df.rename(columns=str.upper)
print(df)

Output:

   X  Y
0  1  4
1  2  5
2  3  6

As you can see, all columns have been renamed to uppercase.

That’s how you can use the rename() method to rename columns in Pandas. The following section will cover another way for renaming columns using the columns attribute.

RELATED: How to Automate Tasks with Python

Method 2: Using the columns attribute

Another way to rename columns in Pandas is by directly modifying the columns attribute of the DataFrame. This attribute is a list that contains the column names of the DataFrame, and you can change it to rename columns.

Step-by-step instructions for using the columns attribute to rename columns in Pandas:

Create a Pandas DataFrame that contains the columns you want to rename. For example, let’s say we have the following DataFrame:

import pandas as pd

data = {'name': ['Alice', 'Bob', 'Charlie'], 'age': [25, 30, 35], 'city': ['New York', 'London', 'Paris']}
df = pd.DataFrame(data)
print(df)

Output:

       name  age      city
0     Alice   25  New York
1       Bob   30    London
2  Charlie   35     Paris

To rename columns using the columns attribute, assign the columns attribute a new list of column names. For example, let’s rename the ‘name’ column to ‘full_name’ and the ‘age’ column to ‘years_old’:

df.columns = ['full_name', 'years_old', 'city']
print(df)

Output:

  full_name  years_old      city
0     Alice         25  New York
1       Bob         30    London
2  Charlie         35     Paris

The ‘name’ and ‘age’ columns have been renamed ‘full_name’ and ‘years_old’, respectively.

You can also use list comprehension or any other Pythonic method to rename columns based on specific criteria. For example, let’s say we want to rename all columns to lowercase:

df.columns = [col.lower() for col in df.columns]
print(df)

Output:

  full_name  years_old      city
0     Alice         25  New York
1       Bob         30    London
2  Charlie         35     Paris

As shown above, all column names have been renamed to lowercase.

That’s how you can use the columns attribute to rename columns in Pandas. The following section will cover another method for renaming columns using the set_axis() method.

Related: Guide to Python Libraries for Data Science

Method 3: Using the set_axis() method

The set_axis() method is a built-in function in Pandas that allows you to rename both the row and column indexes of a DataFrame. However, you can also use this method to rename only the column names by specifying the axis parameter as 1.

Step-by-step instructions for using the set_axis() method to rename columns in Pandas:

Create a Pandas DataFrame that contains the columns you want to rename. For example, let’s say we have the following DataFrame:

import pandas as pd

data = {'fruit': ['apple', 'banana', 'cherry'], 'price': [1.99, 0.99, 2.99]}
df = pd.DataFrame(data)
print(df)

Output:

    fruit  price
0   apple   1.99
1  banana   0.99
2  cherry   2.99

To rename columns using the set_axis() method, pass a list of new column names as the labels parameter and set the axis parameter to 1. For example, let’s rename the ‘fruit’ column to ‘product’ and the ‘price’ column to ‘cost’:

df = df.set_axis(['product', 'cost'], axis=1)
print(df)

Output:

  product  cost
0   apple  1.99
1  banana  0.99
2  cherry  2.99

The ‘fruit’ and ‘price’ columns have been renamed ‘product’ and ‘cost’.

You can also use a lambda function to rename columns based on specific criteria. For example, let’s say we want to add the prefix ‘item_’ to all column names:

df = df.set_axis(df.columns.map(lambda x: 'item_' + x), axis=1)
print(df)

Output:

  item_product  item_cost
0        apple       1.99
1       banana       0.99
2       cherry       2.99

All column names have been prefixed with ‘item_’.

That’s how you can use the set_axis() method to rename columns in Pandas. The following section will cover another way for renaming columns using the add_prefix() method.

RELATED: How Much Coding Is Required for Data Science

Method 4: Using the add_prefix() method

The add_prefix() method is a built-in function in Pandas that allows you to add a prefix to all column names in a DataFrame. You can use this method to rename columns by adding a prefix that describes the contents of the columns.

Step-by-step instructions for using the add_prefix() method to rename columns in Pandas:

Create a Pandas DataFrame that contains the columns you want to rename. For example, let’s say we have the following DataFrame:

import pandas as pd

data = {'name': ['John', 'Jane', 'Jim'], 'age': [28, 32, 24]}
df = pd.DataFrame(data)
print(df)

Output:

   name  age
0  John   28
1  Jane   32
2   Jim   24

To rename columns using the add_prefix() method, call the method on the DataFrame object and pass the prefix you want to add as a string parameter. For example, let’s add the prefix ‘person_’ to all column names:

df = df.add_prefix('person_')
print(df)

Output:

  person_name  person_age
0        John          28
1        Jane          32
2         Jim          24

All column names have been renamed with the prefix ‘person_’.

You can also use the add_prefix() method with other string manipulation methods to rename columns based on specific criteria. For example, let’s say we want to add the prefix ‘age_’ to only the ‘age’ column:

df = df.rename(columns={'age': 'age_' + df.columns[1]})
print(df)

Output:

  person_name  age_person
0        John          28
1        Jane          32
2         Jim          24

Only the ‘age’ column has been renamed with the ‘age_’ prefix.

That’s how you can use the add_prefix() method to rename columns in Pandas. The following section will summarize the four ways we’ve covered for renaming columns in Pandas.

Summary

In this article, we’ve covered four different methods for renaming columns in Pandas:

  1. Renaming columns by assigning a new list of column names directly to the DataFrame’s columns attribute.
  2. Renaming columns using the rename() method and passing a dictionary of old and new column names.
  3. Renaming columns using the set_axis() method, passing a list of new column names, and setting the axis parameter to 1.
  4. Renaming columns using the add_prefix() method to add a prefix to all column names in the DataFrame.

Each method has advantages and disadvantages, depending on the specific use case. For example, the rename() method might be the best choice if you need to rename multiple columns in one go. In contrast, if you want to add a prefix to all column names, the add_prefix() method might be more appropriate.

It’s important to note that these methods create a new DataFrame with the updated column names rather than modifying the original DataFrame. Therefore, if you want to alter the original DataFrame, you must assign the new DataFrame to the original variable.

Thanks for reading this article. We hope you have a better understanding of the different methods for renaming columns in Pandas and that you can use them to clean and manipulate your data more effectively.

Leave a Reply