How to Filter Pandas dataframe using ‘IN’ and ‘NOT IN’ like in SQL

Spread the love

Problem –

You are familiar with using IN and NOT IN in SQL and you want to achieve equivalent of it in Pandas.

Solution –

Let’s read a dataset to work with.

import pandas as pd

url = ''
df = pd.read_csv(url)

To apply the logic of IN and NOT IN in pandas we use the isin method.

IN –

Let’s say that we want to get all the rows where Method of Payment is Discover or MasterCard. To do that we can write.

df[df['Method of Payment'].isin(['Discover','MasterCard'])]


For NOT IN we add a tilda sign like this. This will select all the rows where Method of Payment is Not Discover or MasterCard.

df[~df['Method of Payment'].isin(['Discover','MasterCard'])]

Rating: 1 out of 5.

Leave a Reply