Assuming that I have a pandas dataframe df
I can use .loc()
as below:
c1 = df['count'] > 10
c2 = df['min'] > 3
c2 = df['max']> 4
filtered = df.loc[c1 & c2 & c3].T.reset_index()
No say that the conditions are stored in a list:
conditions = [df['count'] > 10, df['min'] > 3, df['max']> 4]
How can I pass this list to the .loc()
and indicate that all conditions must hold (&
)?
You can use functools reduce:
l = [c1,c2,c3]
from functools import reduce
df.loc[reduce(np.logical_and, l)]
@GZ0 improvement...
df.loc[np.logical_and.reduce(l)]
I think np.logical_and.reduce
should work
np.logical_and.reduce(conditions)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With