logo

The isin() Method for Filtering

When filtering for multiple possible values in one column, isin() is cleaner than chaining OR conditions.

Instead of this:

df[(df['city'] == 'NYC') | (df['city'] == 'LA') | (df['city'] == 'Chicago')]

Write this:

df[df['city'].isin(['NYC', 'LA', 'Chicago'])]

Much cleaner, especially as the list grows. You can also negate it with ~:

df[~df['status'].isin(['cancelled', 'refunded'])]

This keeps rows where status is NOT in the exclusion list.

I demonstrate isin() with real datasets in my Pandas course.