In This Section we will be focusing on how to add prefix to the column name in pandas python. There are multiple ways to do it, we will add prefix to all column names in pandas using add_prefix() function and with simple concatenation and list comprehension. we will also add prefix to specific column name in pandas. We will add prefix to all the column name in pandas. let’s look at each of these cases in pandas with an example for each.
- Add prefix to all column name in pandas using add_prefix() function
- Add prefix to all column name in pandas using simple concatenation (+)
- Add prefix to all column name in pandas python using list comprehenstion
- Add prefix 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 to all column name in pandas using add_prefix() function:
In order to add prefix to all column name in pandas we are using add_prefix() function which takes prefix string as argument and will add the prefix to the column name.
#### add prefix to column name in pandas: Method 1 df = df.add_prefix('pre_') df
In the Above example we are adding prefix named “pre” to all column names, so The Resultant dataframe will be
Add prefix to all column name in pandas using simple concatenation:
In order to add prefix to all column name in pandas we are using simple + (concatenation) which concatenates the prefix string and the column names.
#### add prefix to column name in pandas: Method 2 df.columns='pre_'+df.columns df
In the Above example we are adding prefix named “pre” to all column names, so The Resultant dataframe will be
Add prefix to all column name in pandas using list comprehension:
In the below example we are adding prefix to all column name in pandas by using simple + (concatenation), which concatenates the prefix string and the each column names through list comprehension
#### add prefix to column name in pandas: Method 3 List comprehension df.columns = ['pre_'+str(col) for col in df.columns] df
The Resultant dataframe
Add prefix to specific column name in pandas using column index: Method 1
In order to add the prefix to the specific column name in pandas we have used iloc which will subset the specific column for which the prefix is being added.
#### Add Prefix to specific column name in pandas new_names = [(i,'pre_'+i) for i in df.iloc[:, 2:].columns.values] df.rename(columns = dict(new_names), inplace=True) df
In the above example we will be adding the prefix from 3rd column onwards so the Resultant dataframe is
Add prefix to specific column name in pandas using column name:
In order to add prefix to specific column name in pandas we will be using contains() function.
#### Add Prefix to specific column name in pandas using column name cols = df.columns[~df.columns.str.contains('Geography|Science')] df.rename(columns = dict(zip(cols, 'Pre_'+cols)), inplace=True) df
In the above example we will be adding the prefix for the columns which does not contain Geography or Science in their name so the Resultant dataframe is