I wrote a little script that loops through constraints to filter a dataframe. Example and follow up explaining the issue are below.
constraints = [['stand','==','L'],['zone','<','20']]
for x in constraints:
vari = x[2]
df = df.query("{0} {1} @vari".format(x[0],x[1]))
| zone | stand | speed | type | |
|---|---|---|---|---|
| 0 | 2 | L | 83.7 | CH |
| 1 | 7 | L | 95.9 | SI |
| 2 | 14 | L | 94.9 | FS |
| 3 | 11 | L | 93.3 | FS |
| 4 | 13 | L | 86.9 | CH |
| 5 | 7 | L | 96.4 | SI |
| 6 | 13 | L | 82.6 | SL |
I can't figure out a way to filter when there is an OR condition. For example, in the table above I'd like to return a dataframe using the constraints in the code example along with any rows that contain SI or CH in the type column. Does anyone have ideas on how to accomplish this? Any help would be greatly appreciated.
source https://stackoverflow.com/questions/72150521/filter-dataframe-with-multiple-conditions-including-or
Comments
Post a Comment