Remove prefix from column name in pandas python

In This Section we will be focusing on how to remove prefix  from the column name in pandas python. There are multiple ways to do it, we will remove prefix from column name in pandas using lstrip() , rename() and replace() function.  let’s look at each of these cases in pandas with an example for each.

  • Remove prefix from column name in pandas using lstrip() function
  • Remove prefix from column name in pandas using lstrip() and rename() function
  • Remove prefix from the column name in pandas python using replace() function

 

Create Dataframe:

 

#Create a DataFrame

import pandas as pd
import numpy as np

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

df = pd.DataFrame(d,columns=['Name','Maths','pre_Science','pre_Geography'])
df

The Resultant dataframe is

Remove-prefix-from-column-name-in-pandas-python-1

 

 

Remove prefix from column name in pandas python using lstrip() function:

We will be using lstrip() function. Within lstrip() we will be passing the string which will be stripped from left .i.e. prefix is stripped or removed in the column name. so the resultant column name will not have prefix as shown below

 

#### remove prefix from column name in pandas using lstrip() function

df.columns = df.columns.str.lstrip('pre_')
df

The Resultant dataframe with prefix “pre_” removed is

Remove-prefix-from-column-name-in-pandas-python-2

 

 

Remove prefix from column name in pandas using lstrip() and rename() function:

We will be using lstrip() function along with rename() function. Within lstrip() we will be passing the string which will be stripped from left .i.e. prefix is stripped or removed in the column name. This is done for each column using lambda and the column name will be renamed using rename() function  as shown below

 
#### remove prefix from column name in pandas using lstrip() and rename() function

df=df.rename(columns = lambda x: x.lstrip('pre_'))
df

The Resultant dataframe with prefix “pre_” removed and renamed is

Remove-prefix-from-column-name-in-pandas-python-2

 

 

Remove prefix from column name in pandas using replace() function:

We will be using replace() function to remove prefix from column name.  This is the most easiest method we will simple replacing the prefix value with empty value  through replace() function for all the columns in the dataframe. So all the column name with desired prefix will be replaced using replace() function  as shown below

 

### Remove prefix from the column name in pandas using replace() function

df.columns = [col.replace('pre_', '') for col in df.columns]
df

 

The Resultant dataframe with prefix “pre_” replaced with “” is

Remove-prefix-from-column-name-in-pandas-python-2

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts