Add suffix to column name in pandas python

In This Section we will be focusing on how to add suffix to the column name in pandas python. There are multiple ways to do it, we will add suffix to all column names in pandas using add_suffix() function and with simple concatenation and list comprehension. we will also add suffix to specific column name in pandas. We will add suffix to all the column name in pandas. let’s look at each of these cases in pandas with an example for each.

  • Add suffix to all column name in pandas using add_suffix() function
  • Add suffix to all column name in pandas using simple concatenation (+)
  • Add suffix to all column name in pandas python using list comprehenstion
  • Add suffix to specific column name in pandas

 

Create Dataframe:

 

## create dataframe

import pandas as pd
import numpy as np

d = { 'Name':['Alisa','Bobby','Cathrine','Jodha','Raghu','Ram'],
'Maths':[76,73,83,93,89,94],
'Science':[85,41,55,75,81,97],
'Geography':[78,65,55,88,87,98]}

df = pd.DataFrame(d,columns=['Name','Maths','Science','Geography'])
df

The Resultant dataframe

Add prefix and suffix to column name in pandas python 1

 

 

Add suffix to all column name in pandas using add_suffix() function:

In order to add suffix to all column name in pandas we are using add_suffix() function which takes suffix string as argument and will add the suffix to the column name.

 
#### add suffix to column name in pandas: Method 1

df = df.add_suffix('_x')
df

In the Above example we are adding suffix named “x” to all column names, so The Resultant dataframe will be

Add prefix and suffix to column name in pandas python 3

 

 

 

Add suffix to all column name in pandas using simple concatenation:

In order to add suffix to all column name in pandas we are using simple + (concatenation)  which concatenates the column names and the suffix string

 
#### add suffix to column name in pandas: Method 2

df.columns += '_x'
df

In the Above example we are adding suffix named “x” to all column names, so The Resultant dataframe will be

Add prefix and suffix to column name in pandas python 3

 

 

 

Add suffix to all column name in pandas using list comprehension:

In the below example we are adding suffix to all column name in pandas by using simple + (concatenation), which concatenates the suffix string and the each column names through list comprehension

 
#### add suffix to column name in pandas: Method 3 List comprehension


df.columns = [str(col) + '_x' for col in df.columns]
df

The Resultant dataframe

Add prefix and suffix to column name in pandas python 3

 

 

 

Add suffix to specific column name in pandas using column index:

In order to add the suffix to the specific column name in pandas we have used iloc which will subset the specific column for which the suffix is being added.

 
#### Add suffix to specific column in pandas

new_names = [(i,i+'_Post') for i in df.iloc[:, 2:].columns.values]

df.rename(columns = dict(new_names), inplace=True)
df

In the Above example we are adding suffix named “post” to all column names, so The Resultant dataframe will be

Add prefix and suffix to column name in pandas python 3b

 

 

 

Add suffix to specific column name in pandas using column name:

In order to add suffix to specific column name in pandas we will be using contains() function.

 
#### Add suffix to specific column in pandas

cols = df.columns[~df.columns.str.contains('Geography|Science')]

df.rename(columns = dict(zip(cols, cols+'_Post')), inplace=True)
df

In the above example we will be adding the suffix for the columns which does not contain Geography or Science in their name so the Resultant dataframe is

Add prefix and suffix to column name in pandas python 3c