Created
February 16, 2021 23:02
-
-
Save ayoskovich/8f46ff46fa373812f087967aaf0530f8 to your computer and use it in GitHub Desktop.
Filter pandas on mult criteria
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
https://pandas.pydata.org/pandas-docs/stable/user_guide/cookbook.html#building-criteria