Home > Back-end >  How to add prefix to column names except some columns?
How to add prefix to column names except some columns?

Time:12-11

This is an adaptation of a question posed by @ScalaBoy here and answered by @timgeb, the question is the same, except it is about a prefix not suffix:

Given pandas DataFrame, how can I add the prefix "new_" to all columns except two columns Id and Name?

import pandas as pd
data = [[1,'Alex',22,'single'],[2,'Bob',32,'married'],[3,'Clarke',23,'single']]
df = pd.DataFrame(data,columns=['Id','Name','Age','Status'])

CodePudding user response:

You can use rename method:

df.rename({col:'new_'  col for col in df.columns[~df.columns.isin(['Id','Name'])]}, axis=1)

CodePudding user response:

Use add_prefix. This function is applied to the whole dataframe. To hide columns you dont need renamed, set them as index. After renaming you can reset back the index. Code below

df=df.set_index(['Id', 'Name']).add_prefix('new_').reset_index()



    Id    Name  new_Age new_Status
0   1    Alex       22     single
1   2     Bob       32    married
2   3  Clarke       23     single

CodePudding user response:

Another option, use list comprehension on the dataframe column header object, this way you aren't manipulating nor copying the entire dataframe:

df.columns = [f'new_{i}' if i not in ['Name', 'Id'] else f'{i}' for i in df.columns]

df

Output:

   Id    Name  new_Age new_Status
0   1    Alex       22     single
1   2     Bob       32    married
2   3  Clarke       23     single

CodePudding user response:

df.rename(columns = lambda col: f"new_{col}" 
                                if col not in ('Name', 'Id') 
                                else col
          )

   Id    Name  new_Age new_Status
0   1    Alex       22     single
1   2     Bob       32    married
2   3  Clarke       23     single

You could achieve it with regex; however, in my opinion, it is not as clear as the previous solution:


new_col = df.columns.str.replace(pat = r"\b(?!Id|Name\b). ", 
                                 repl = lambda m: f"new_{m[0]}", 
                                 regex=True)

df.set_axis(new_col, axis=1) # or df.columns = new_col

   Id    Name  new_Age new_Status
0   1    Alex       22     single
1   2     Bob       32    married
2   3  Clarke       23     single
  • Related