Skip to content Skip to sidebar Skip to footer

Nested Queries / Comparing Multiple Datasets Efficiently In Pandas

I am using Pandas (first time) to determine whether personnel meet prerequisites when it comes to course attendance. The code below returns the desired results however I am sure th

Solution 1:

pivoted = df.groupby(['Name', df.CourseName.str.split().str[0]]) \
            .CourseId.size().gt(0).unstack(fill_value=False)

pivoted

enter image description here

matches = pivoted.query('Engineering & Mathematics & ~Physics')
matches

enter image description here

df.query('Name in @matches.index')

enter image description here


Solution 2:

Use query to type more natural math relationships :

df.query('CourseId == "P12" or CourseId != "P99"')

Post a Comment for "Nested Queries / Comparing Multiple Datasets Efficiently In Pandas"