Pandas – Add Prefix to Column Names

In this tutorial, we will look at how to add a prefix to the column names in a pandas dataframe with the help of some examples.

How to add a prefix to each column name of a pandas dataframe?

add prefix to pandas dataframe column names

You can use the built-in pandas dataframe add_prefix() function to add a prefix to the column names of a dataframe. Pass the prefix string as an argument.

The following is the syntax –

# add prefix to column names
df = df.add_prefix(prefix)

It returns the dataframe (or series if applied on a pandas series) with the updated labels.

Alternatively, you can use a list comprehension to create a list of new column names with the prefix added.

Examples

Let’s now look at some examples of using the above methods.

First, we will create a dataframe that we will be using throughout this tutorial.

import pandas as pd

# employee data
data = {
    "Name": ["Jim", "Dwight", "Angela", "Tobi"],
    "Age": [26, 28, 27, 32]
}

# create pandas dataframe
df = pd.DataFrame(data)

# display the dataframe
df

Output:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

pandas dataframe of employees with "Name" and "Age" columns

Here, we created a dataframe with information about some employees in an office. The dataframe has the following columns – “Name” and “Age”.

Example 1 – Add prefix to column names using add_prefix()

Let’s now add the prefix “Employee_” to the column names using the pandas dataframe add_prefix() function.

# add prefix to column names
df = df.add_prefix("Employee_")
# display the dataframe
df

Output:

"Employee_" prefix added to dataframe column names

The column names of the dataframe are now prefixed with “Employee_”.

Example 2 – Add prefix to column names using a list comprehension

Alternatively, you can use a list comprehension to create a new list of column names with the prefix added.

For example, let’s take the same use-case from the above example. We will reset the column names to their original values and then add the prefix to column names using a list comprehension.

# reset column names to original values
df.columns = ["Name", "Age"]
# add prefix to column names
df.columns = ["Employee_"+str(col) for col in df.columns]
# display the dataframe
df

Output:

"Employee_" prefix added to dataframe column names

We get the same result as above.

You can further simplify this step by directly adding the prefix to df.columns.

# reset column names to original values
df.columns = ["Name", "Age"]
# add prefix to column names
df.columns = "Employee_" + df.columns
# display the dataframe
df

Output:

"Employee_" prefix added to dataframe column names

We get the same result as above. The prefix “Employee_” is added to each column name in the dataframe.

Summary

In this tutorial, we looked at how to add a prefix to each column name of a pandas dataframe. The following are the key takeaways.

  • You can use the pandas dataframe built-in add_prefix() function to add a prefix to the column names.
  • Alternatively, you can use a list comprehension to modify the column names with the added prefix.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top