Reputation: 27
I have a dataframe that I am trying to filter in many different ways. I need to assign the various columns and operators as variables being called from a list of combinations. I cannot find a way to insert the operator variables into the dataframe filter.
import itertools
import operator
import pandas as pd
df = pd.DataFrame(columns = ['Open','High','Low','Close']
rollingList = [5,10,20,30,60,90,180,365]
for i in rollingList:
df[str(i)+' rolling day high'] = df['High'].rolling(i).max()
df[str(i)+' rolling day low'] = df['Low'].rolling(i).min()
columnListRolling = [i for i in df.columns if 'day high' in i]
possibleCols1 = ['Open','High','Low','Close']
opp1 = ['>','<']
possibleCols2 = ['Open','High','Low','Close']
possibleCols3 = ['Open','High','Low','Close']
opp4 = ['>','<']
combo = list(itertools.product(possibleCols1,opp1,possibleCols2,possibleCols3,opp4,columnListRolling))
# instead of ['>','<'], I have also tried replacing with operator.gt/operator.lt
#a- always a column
#b-always either < or >
#c- always a column
#d- always a column
#e-always either < or >
#f- always a column
for a,b,c,d,e,f in combo:
filter = df[(df[a] b df[c]) & (df[d] e df[f].shift(1))]
#I have also tried using .query with no success
filter2 = df.query('(a+b+c)&(d+e+f)')
# I am then iterating through each index of the filter df
Upvotes: 0
Views: 776
Reputation: 23753
With
opp1=[operator.gt,operator.lt]
opp4=[operator.gt,operator.lt]
Use
filter = b(df[a],df[c]) & (e(df[d],df[f])).shift(1)
Upvotes: 1
Reputation: 25259
You can do this with query():
filter2 = df.query(f'(`{a}`{b}`{c}`) & (`{d}`{e}`{f}`)')
Upvotes: 2