One possible way is to define a check function of your own and perform apply
on the dataframe.
For example, if you know the list of combinations that need to be filtered (this list can be extracted beforehand from a dataframe):
filter_list_multicols = [["book", "cat"], ["table", "dog"], ["table", "cat"], ["pen", "horse"], ["book", "horse"]]
Then you could define a check function as so:
def isin_multicols_check(stationary_name, animal_name):
for filter_pair in filter_list_multicols:
if (stationary_name == filter_pair[0]) and (animal_name == filter_pair[1]):
return True
return False
Example dataframe:
df = pd.DataFrame([
[1, "book", "dog"], [2, "pen", "dog"], [3, "pen", "rat"], [4, "book", "horse"], [5, "book", "cat"]
],
columns=["S.N.", "stationary_name", "animal_name"])
df
S.N. stationary_name animal_name
1 book dog
2 pen dog
3 pen rat
4 book horse
5 book cat
And now, call the function using pandas apply
:
df["is_in"] = df.apply(lambda x: isin_multicols_check(x.stationary_name, x.animal_name), axis=1)
df
S.N. stationary_name animal_name is_in
1 book dog false
2 pen dog false
3 pen rat false
4 book horse true
5 book cat true
The result:
is_in = df[df["is_in"]==True]
not_is_in = df[df["is_in"]==False]