Filter Pandas Dataframe using an arbitrary number of conditions
I believe that reduce( (lambda x, y: x & (df[y[0]]<y[1])), list_of_filters ) will do it.
I am comfortable with basic filtering and querying using Pandas. For example, if I have a dataframe called df I can do df[df['field1'] < 2] or df[df['field2'] < 3]. I can also chain multiple criteria together, for example:
df[(df['field1'] < 3) & (df['field2'] < 2)].
What if I don't know in advance how many criteria I will need to use? Is there a way to "chain" an arbitrary number of these operations together? I would like to pass a list of filters such as [('field1', 3), ('field2', 2), ('field3', 4)] which would result in the chaining of these 3 conditions together.
Thanks!