You can use pd.Series.isin.
For “IN” use: something.isin(somewhere)
Or for “NOT IN”: ~something.isin(somewhere)
As a worked example:
import pandas as pd
>>> df
country
0 US
1 UK
2 Germany
3 China
>>> countries_to_keep
[‘UK’, ‘China’]
>>> df.country.isin(countries_to_keep)
0 False
1 True
2 False
3 True
Name: country, dtype: bool
>>> df[df.country.isin(countries_to_keep)]
country
1 UK
3 China
>>> df[~df.country.isin(countries_to_keep)]
country
0 US
2 Germany
Alternative solution that uses .query() method:
In [5]: df.query(“countries in @countries_to_keep”)
Out[5]:
countries
1 UK
3 China
In [6]: df.query(“countries not in @countries_to_keep”)
Out[6]:
countries
0 US
2 Germany