Skip to content

Instantly share code, notes, and snippets.

@ayoskovich
Created February 16, 2021 23:02
Show Gist options
  • Save ayoskovich/8f46ff46fa373812f087967aaf0530f8 to your computer and use it in GitHub Desktop.
Save ayoskovich/8f46ff46fa373812f087967aaf0530f8 to your computer and use it in GitHub Desktop.
Filter pandas on mult criteria
df = pd.DataFrame({"AAA": [4, 5, 6, 7], "BBB": [10, 20, 30, 40], "CCC": [100, 50, -30, -50]})
Crit1 = df.AAA <= 5.5
Crit2 = df.BBB == 10.0
Crit3 = df.CCC > -40.0
# Could do this
# AllCrit = Crit1 & Crit2 & Crit3
# But this is better
import functools
CritList = [Crit1, Crit2, Crit3]
AllCrit = functools.reduce(lambda x, y: x & y, CritList)
df[AllCrit]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment