Search code snippets, questions, articles...

Add suffix/prefix to column names of Pandas DataFrame

If you have a pandas DataFrame and want to add a suffix or prefix to the column name, there are a couple of ways to do it. In this post, we will describe them one by one.
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

First, let's create a DataFrame

import pandas as pd

# create a dataframe
df = pd.DataFrame({
    'name': ['Rick', 'Carol', 'Daryl', 'Negan'],
    'place': ['Alexendria', 'Kingdom', 'Hilltop', 'Saviours'],
    'episode': [50, 30, 35, 29]
})

print(df)
╒════╤════════╤════════════╤═══════════╕
│    │ name   │ place      │   episode │
╞════╪════════╪════════════╪═══════════╡
│  0 │ Rick   │ Alexendria │        50 │
├────┼────────┼────────────┼───────────┤
│  1 │ Carol  │ Kingdom    │        30 │
├────┼────────┼────────────┼───────────┤
│  2 │ Daryl  │ Hilltop    │        35 │
├────┼────────┼────────────┼───────────┤
│  3 │ Negan  │ Saviours   │        29 │
╘════╧════════╧════════════╧═══════════╛

In the above DataFrame, we want to add a suffix or prefix to the column names. We can use the below methods in order to do that.

Solution 1: Use DataFrame.add_suffix() function to add a suffix to all column names

A DataFrame is a two-dimensional data structure that contains columns and rows. The add_suffix() function is used to add a suffix to all column names in a DataFrame. This function is useful when we want to add a specific character to all column names in a DataFrame, such as an underscore "_".

Code example

import pandas as pd

# create a dataframe
df = pd.DataFrame({
    'name': ['Rick', 'Carol', 'Daryl', 'Negan'],
    'place': ['Alexendria', 'Kingdom', 'Hilltop', 'Saviours'],
    'episode': [50, 30, 35, 29]
})

# add suffix to each column
df = df.add_suffix('_watch')

print(df)

Output

╒════╤══════════════╤═══════════════╤═════════════════╕
│    │ name_watch   │ place_watch   │   episode_watch │
╞════╪══════════════╪═══════════════╪═════════════════╡
│  0 │ Rick         │ Alexendria    │              50 │
├────┼──────────────┼───────────────┼─────────────────┤
│  1 │ Carol        │ Kingdom       │              30 │
├────┼──────────────┼───────────────┼─────────────────┤
│  2 │ Daryl        │ Hilltop       │              35 │
├────┼──────────────┼───────────────┼─────────────────┤
│  3 │ Negan        │ Saviours      │              29 │
╘════╧══════════════╧═══════════════╧═════════════════╛

The code example above creates a DataFrame with four columns and four rows. The columns are 'name', 'place', and 'episode'. The rows are labeled 'Rick', 'Carol', 'Daryl', and 'Negan'. The code then adds a suffix to each column header. The suffix is '_watch'.

Solution 2: Add prefix to all column names using add_prefix() function

The Pandas add_prefix() function is used to prefix labels of columns in a DataFrame with a string. The function takes a string as an argument and returns a new DataFrame with the columns labeled with the given string as a prefix.

Syntax

DataFrame.add_prefix('prefix_name')

Code example

import pandas as pd

# create a dataframe
df = pd.DataFrame({
    'name': ['Rick', 'Carol', 'Daryl', 'Negan'],
    'place': ['Alexendria', 'Kingdom', 'Hilltop', 'Saviours'],
    'episode': [50, 30, 35, 29]
})

# add prefix to each column
df = df.add_prefix('pre_')

print(df)

Output

╒════╤════════════╤═════════════╤═══════════════╕
│    │ pre_name   │ pre_place   │   pre_episode │
╞════╪════════════╪═════════════╪═══════════════╡
│  0 │ Rick       │ Alexendria  │            50 │
├────┼────────────┼─────────────┼───────────────┤
│  1 │ Carol      │ Kingdom     │            30 │
├────┼────────────┼─────────────┼───────────────┤
│  2 │ Daryl      │ Hilltop     │            35 │
├────┼────────────┼─────────────┼───────────────┤
│  3 │ Negan      │ Saviours    │            29 │
╘════╧════════════╧═════════════╧═══════════════╛

The above code example creates a DataFrame with four columns ('name', 'place', 'episode') and four rows of data. It then adds a prefix to each column in the DataFrame. The resulting DataFrame is printed.

If you want to change the names of a few columns then you can read the below articles.

Change the name of columns in a pandas dataframe

import pandas as pd

# create a dataframe
df = pd.DataFrame({
    'name': ['Rick', 'Carol', 'Daryl', 'Negan'],
    'place': ['Alexendria', 'Kingdom', 'Hilltop', 'Saviours'],
    'episode': [50, 30, 35, 29]
})

# add suffix
df = df.add_suffix('_watch')
print(df)
Was this helpful?
0 Comments