You are familiar with using IN and NOT IN in SQL and you want to achieve equivalent of it in Pandas.
Let’s read a dataset to work with.
import pandas as pd url = 'https://raw.githubusercontent.com/bprasad26/lwd/master/data/clothing_store_sales.csv' df = pd.read_csv(url) df.head()
To apply the logic of IN and NOT IN in pandas we use the isin method.
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'])]
NOT IN –
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'])]