I have a dataframe with about 600 rows, dating back to 2015:
ID Year Trap SVL
1 2020 ['P90', 'R81'] 78
2 2019 ['P3'] 56
3 2018 ['P22', 'R1', 'R11', 'PR22'] 45
I need to search/filter individuals at hand (lizards), the known variables are the Trap and SVL (snout to vent length in mm) - for example, the current lizard might be from Trap == 'R1' and its SVL >= 45, then the search should return ID 3. In 2021, the lizard has an SVL = 75 (it grows by ~10mm/yr and I add +/- 5mm for measurement error).
I keep the criteria in a dict:
dict_choice = {'Trap': ['R1'],
'SVL': (70.0, 80.0)}
My normal search is the following:
newdf = df.loc[df.Trap.map(set(dict_choice['Trap']).issubset) &
( (df['SVL'] >= dict_choice['SVL'][0]) & (df['SVL'] <= dict_choice['SVL'][1]))
Can I include the years as a search criteria? To minimise the number of matching lizards, I need to add a filter where for every year going back from 2021, we look through SVLs which are 5mm less. E.g lizard ID 3 was 45mm in 2018, which means that in 2021 it should be 75mm, however to match it we only need to look at lizars with SVL<=45. Something like this logic (if you can call it that!):
if @year == 2021:
search all where @Trap IN dict_choice['Trap'] AND ((df['SVL'] >= dict_choice['SVL'][0]) & (df['SVL'] <= dict_choice['SVL'][1]))
elif @year == 2020:
search all where @Trap IN dict_choice['Trap'] AND ((df['SVL'] >= dict_choice['SVL'][0]) & (df['SVL'] <= dict_choice['SVL'][1]-5))
elif @year == 2019:
search all where @Trap IN dict_choice['Trap'] AND ((df['SVL'] >= dict_choice['SVL'][0]) & (df['SVL'] <= dict_choice['SVL'][1]-10))
(...)
TL;DR: Can I include an if statement when filtering a dataframe by other criteria?