Let's say I have a dataframe df
with an arbitrary number of columns. As an example, say we have
a b c
0 5 foo 2
1 5 bar 3
2 4 foo 2
3 5 test 1
4 4 bar 7
Suppose I want a filter like
df[(df['a'] == 5) & (~df['b'].isin(['foo','bar'])) & (df['c'].isin(range(5)))]
or maybe something like
df[(df['a'] == 5) & (~df['b'].isin(['test','bar'])) | (df['c'].isin(range(5)))]
but I want something that can easily be plugged in as an input, something like:
def filter_df(filter_kwargs, df):
# do the filtering here
I have an idea what to do with the ==
operator, but I'm perplexed how to do more complicated ones, like .isin
and |
. What's the best way to do this?