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
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:
new_df = df.loc[df.column == 'value']
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:
new_df = df.loc[(df.this == True) & (df.that == True)]
Now let’s pretend that list of filter attributes grows a little bit. Instead of 2 attributes to filter on, you have 4:
list_of_data = ['this','that','something else','another thing']
Using the .loc approach, you might come up with something like this:
new_df = df.loc[(df.column == 'this') | (df.column == 'that') | (df.column == 'something else') | (df.column == 'another thing')]
This approach above is doable for 1–2 attributes — maybe even 3 — but becomes much too time consuming…