首页 > 解决方案 > 如何使用运算符作为在 for 循环中调用的变量来过滤熊猫数据框

问题描述

我有一个数据框,我试图以多种不同的方式进行过滤。我需要将各种列和运算符分配为从组合列表中调用的变量。我找不到将运算符变量插入数据框过滤器的方法。

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 

标签: pythonpandasoperatorscombinationsitertools

解决方案


您可以使用 query() 执行此操作:

filter2 = df.query(f'(`{a}`{b}`{c}`) & (`{d}`{e}`{f}`)')

推荐阅读