pandas数据帧中条件过滤的泛型函数

2024-05-28 20:09:47 发布

您现在位置:Python中文网/ 问答频道 /正文

样品过滤条件:-在

enter image description here 数据

x  y  z 
1  2  1
1  3  2
1  2  5
1  3  1

现在我想从给定的数据中过滤上述指定的条件。 为此,我需要一个通用函数,也就是说,该函数应该适用于任何过滤器,而不仅仅适用于上述指定的过滤器。在

我知道如何在python中为多个条件手动过滤数据。在

我认为泛型函数可能需要两个参数一个是数据,另一个是过滤条件。在

但是我找不到编写通用函数来过滤数据的逻辑。在

好心的人可以帮我解决。在

提前谢谢。


Tags: 数据函数过滤器参数样品手动逻辑条件
3条回答

由于只有一个数字dtype,因此可以使用底层NumPy数组:

res = df[(df.values == [1, 2, 1]).all(1)]

print(res)

   x  y  z
0  1  2  1

对于具有list输入的泛型函数:

^{pr2}$

如果需要字典输入:

def filter_df(df, d):
    L = list(map(d.get, df))
    return df[(df.values == L).all(1)]

res = filter_df(df, {'x': 1, 'y': 2, 'z': 1})

您可以创建conditions和{a1}的列表:

x1 = df.x==1
y2 = df.y==2 
z1 = df.z==1
y3 = df.y==3

m1 = np.logical_and.reduce([x1, y2, z1])
m2 = np.logical_and.reduce([x1, y3, z1])

或者^{}all mask来检查^{}每行的所有True

^{pr2}$

编辑:

如果可能,请使用字典中的筛选值定义列名:

d1 = {'x':1, 'y':2, 'z':1}
d2 = {'x':1, 'y':3, 'z':1}

m1 = np.logical_and.reduce([df[k] == v for k, v in d1.items()])
m2 = np.logical_and.reduce([df[k] == v for k, v in d2.items()])

另一种使用merge的方法,使用从字典创建的一行数据帧:

df1 = pd.DataFrame([d1]).merge(df)

编辑:

对于一般解决方案,可以将文件的每个值解析为元组并使用operators

df1 = pd.DataFrame({0: ['x==1', 'x==1'], 1: ['y==2', 'y<=3'], 2: ['z!=1', 'z>1']})
print (df1)
      0     1     2
0  x==1  y==2  z!=1
1  x==1  y<=3   z>1


import operator, re

ops = {'>': operator.gt,
        '<': operator.lt,
       '>=': operator.ge,
       '<=': operator.le,
       '==': operator.eq,
        '!=': operator.ne}

#if numeric, parse to float, else not touch ()e.g. if string
def try_num(x):
    try:
        return float(x)
    except ValueError:
        return x

L = df1.to_dict('r')
#https://stackoverflow.com/q/52620865/2901002
rgx = re.compile(r'([<>=!]+)')
parsed = [[rgx.split(v) for v in d.values()] for d in L]
L = [[(x, op, try_num(y)) for x,op,y in ps] for ps in parsed]
print (L)
[[('x', '==', 1.0), ('y', '==', 2.0), ('z', '!=', 1.0)], 
 [('x', '==', 1.0), ('y', '<=', 3.0), ('z', '>', 1.0)]]

现在按list的第一个值过滤-文件的第一行:

m = np.logical_and.reduce([ops[j](df[i], k) for i, j, k in L[0]])
print (m)
[False False  True False]
def filter_function(df,filter_df):
  lvl_=list()
  lvl=list()
  vlv=list()
  df1=pd.DataFrame()
  n=filter_df.apply(lambda x: x.tolist(), axis=1)
  for i in range(0,len(n)):
      for j in range(0,len(n[i])):
          if i==0:
             lvl_.append(n[i][j].split('==')[0])
          lvl.append(n[i][j].split('==')[1])
          if len(lvl)==len(n[i]):
             vlv.append(lvl)
             lvl=list()
  final_df=df[lvl_]
  for k in range(0,len(vlv)):
      df1=df1.append(final_df[final_df.isin(vlv[k])].dropna())
  return(df1)

filter_function(df,filter_df)

相关问题 更多 >

    热门问题