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

190
Vistas
python pandas "groupby" and "if any" condition

I have a dataframe similar to the one below, and I would like to create a new variable which contains true/false if for each project the sector "a" has been covered at least once. I'm trying with the group.by() function, and wanted to use the .transform() method but since my data is text, I don't know how to use it.

      project    sector  
    
        01         a    
        01         b    
        02         b     
        02         b     
        03         a     
        03         a     
    



 project    sector   new_col

    01         a     true
    01         b     true
    02         b     false
    02         b     false
    03         a     true
    03         a     true

       
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

It's not the fastest option, but definitely should work.

new_col = your_db.groupby(['project'])['sector'].unique().apply(lambda x: 'a' in x).rename('new_col')
your_db = your_db.merge(new_col, how = 'inner', left_on = 'project', right_on = 'project')
over 4 years ago · Santiago Trujillo Denunciar

0

You could try the following:

df['new_col'] = df.groupby('project')['sector'].transform(lambda x: (x == 'a').any() )

This will group by project and check if any 'a' is in the groups sectors

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