How can I filter a Pandas dataframe using 'in' and 'not in' operators similar to SQL?

I am working on a data analysis project, and I have a large Pandas dataframe containing a lot of information. I need to filter this data frame based on specific values of a column, similar to the way we use the in and not in operators in SQL.

After some research, I found out that Pandas offers an easy way to achieve this using the isin() and ~isin() methods. However, I am still having some trouble implementing this in my code.

Here is an example data frame:

 import pandas as pd
 df = pd.DataFrame({'Name': ['John', 'Jane', 'Bob', 'Alice'],
                     'Age': [25, 30, 35, 40],
                     'City': ['New York', 'London', 'Paris', 'Tokyo']})

I want to filter this data frame to get all rows where the city column contains either ‘New York’ or ‘Paris’. In SQL, I would do something like:

SELECT * FROM df WHERE City IN ('New York', 'Paris');

How can I achieve the same result in Pandas using the isin() method? Additionally, are there any other ways to achieve this filtering using Pandas? I would appreciate any help or advice on this topic.

Thank you!

1 Like