Image by 95C from Pixabay

Using Pandas’ .loc and .isin() to Filter for a List of Values in Python

Documentation for using both .isin() and .loc to filter a Pandas Dataframe on a list of values

David Allen
2 min readJul 19, 2021

--

I use .loc on a daily basis. It’s like using the filter function on a spreadsheet.

It’s an effortless way to filter down a Pandas Dataframe into a smaller chunk of data.

It typically works like this:

Sometimes, you’ll want to filter by a couple of conditions. Let’s pretend you want to filter down where this is true and that is true. You might write:

Now let’s pretend that list of filter attributes grows a little bit. Instead of 2 attributes to filter on, you have 4:

Using the .loc approach, you might come up with something like this:

This approach above is doable for 1–2 attributes — maybe even 3 — but becomes much too time consuming…

--

--

David Allen

Documentation and tutorials on Python, Pandas, Jupyter Notebook, and Data Analysis.