Pandasdrill

Level 1: Selecting and filtering Exercise 10 of 63 Pro

Exclude a group with ~

A pandas exercise from Pandasdrill. Solve it with a Python editor in your browser running real pandas, and it is checked the moment you submit.

The task

Return the employees who are not in Engineering and have at least 3 years of service. Keep all columns and the original row labels.

What this exercise teaches

~mask is True where the mask was False, so ~(dept == "Engineering") selects every other department. Because ~ binds tighter than ==, the comparison needs its own parentheses. Negating the whole combined condition would instead keep engineers with few years, which is a different question.

A hint if you get stuck

The ~ operator flips a boolean mask. Wrap each condition in parentheses before combining with &.

Try it now, free, no account needed

The first 15 tasks on Pandasdrill are free. Open it on the practice page, solve it, and get instant feedback with a hint. A free account adds the worked solution; Pro adds timed mode and the certificate.

Open this exercise

More exercises in Selecting and filtering