Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

823
Vistas
How to add prefix to column names except some columns?

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'])
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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
over 4 years ago · Santiago Trujillo Denunciar

0

You can use rename method:

df.rename({col:'new_'+ col for col in df.columns[~df.columns.isin(['Id','Name'])]}, axis=1)
over 4 years ago · Santiago Trujillo Denunciar

0

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
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda