Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove group if contains record with status 300

I would like to group records by ID from df and delete group if any of records has 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],
)

output:

   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

I would like to get:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   ID  STATUS
0  A1     100
1  A1     100
2  A1     100

I tried:
dfnew = df1.groupby(‘ID’).filter(lambda x: x[‘STATUS’] != 300)

But I got the error:
TypeError: filter function returned a Series, but expected a scalar bool

>Solution :

df1.groupby('ID').filter(lambda x: 300 not in x['STATUS'].to_list())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading