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

Pandas Intersection after grouping based on common errors between each group

I have the following dataframe:

Dataframe

I want to find intersections based on ID that consistently have errors in all the Run.
So, all IDs are repeating in all Runs. I tried to group data by Run first, then as per this similar question. I tried the code, but it doesn’t return an intersection.

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

 filter=all_data_df.groupby('Run')['Error'].transform('nunique') == all_data_df['Error'].nunique()
 df = all_data_df.loc[filter]

The results is same dataframe I started with.
How can this be fixed?

I am expecting to obtain

enter image description here

Where only ID 234534 consistently has errors.

>Solution :

You can use boolean indexing to keep only the Ids that have errors in all runs :

# Below, I'm using `df` instead of `all_data_df`

consistent_errors = df["Error"].eq("Yes").groupby(df["Id"]).transform("all")

out = df.loc[consistent_errors]
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