Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
Eliminar grupo si contiene registro con estado 300

Me gustaría agrupar registros por ID de df y eliminar el grupo si alguno de los registros tiene STATUS = 300.

 import pandas as pd df1 = pd.DataFrame( { "ID": ["A0", "A0", "A0", "A1", "A1", "A1", "A2", "A2", "A2"], "STATUS": [100, 100, 300, 100, 100, 100, 300, 100, 100], }, index=[0, 1, 2, 3, 4, 5, 6, 7, 8], )

producción:

 ID STATUS 0 A0 100 1 A0 100 2 A0 300 3 A1 100 4 A1 100 5 A1 100 6 A2 300 7 A2 100 8 A2 100

Me gustaría conseguir:

 ID STATUS 0 A1 100 1 A1 100 2 A1 100

Probé: dfnew = df1.groupby('ID').filter(lambda x: x['STATUS'] != 300)

Pero recibí el error: TypeError: la función de filtro devolvió una serie, pero esperaba un bool escalar

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

df1.groupby('ID').filter(lambda x: 300 not in x['STATUS'].to_list())
over 4 years ago · Santiago Trujillo Report

0

Un método eficiente para hacer coincidir cualquier valor de una lista (ver el comentario de OP) es usar isin junto con groupby + transform :

 df1[~df1['STATUS'].isin([300, 500]).groupby(df1['ID']).transform('any')]

producción:

 ID STATUS 3 A1 100 4 A1 100 5 A1 100
over 4 years ago · Santiago Trujillo Report

0

Otra forma de resolverlo es con transform . Creamos una máscara de ID de grupo donde su máximo no es 300. También podríamos usar not in o cualquier lógica que decidamos

 df1[df1.groupby('ID')['STATUS'].transform(lambda d: d.max() != 300)] # or this for 300 + 500 df1[df1.groupby('ID')['STATUS'].transform(lambda d: d.max() not in (300, 500))]
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!