Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何动态创建'&';数据帧的可变长度过滤器_Python_Python 3.x_Pandas - Fatal编程技术网

Python 如何动态创建'&';数据帧的可变长度过滤器

Python 如何动态创建'&';数据帧的可变长度过滤器,python,python-3.x,pandas,Python,Python 3.x,Pandas,守则的目的: 根据来自主数据帧(df)的长度可变的输入(测试元组和参数),生成过滤数据帧(过滤数据帧)。可能有数百种过滤器组合 任职理由: 我想说的是,这里的一切都能产生预期的产出。也就是说,我不喜欢解决方案1的实现方法,即在dict中创建一个虚拟DataFrame对象,循环会依次过滤和更新该对象。解决方案1似乎有点含糊不清,但我需要一些指导来实现更简洁的内容 请求: 有没有办法使用解决方案2中所示的过滤器 筛选器\u t格式正确,但为字符串。是否有一种方法可以产生过滤器,以便按图所示使用 输入

守则的目的:

根据来自主数据帧(df)的长度可变的输入(测试元组和参数),生成过滤数据帧(过滤数据帧)。可能有数百种过滤器组合

任职理由:

我想说的是,这里的一切都能产生预期的产出。也就是说,我不喜欢解决方案1的实现方法,即在dict中创建一个虚拟DataFrame对象,循环会依次过滤和更新该对象。解决方案1似乎有点含糊不清,但我需要一些指导来实现更简洁的内容

请求:

有没有办法使用解决方案2中所示的过滤器

筛选器\u t格式正确,但为字符串。是否有一种方法可以产生过滤器,以便按图所示使用

输入示例:

test_tuple = [('Serial Number', [12345]),
              ('Test Points', ['TestpointA', 'TestpointC']),
              ('Voltage_1', [3.0, 3.3, 3.6, 0.0]),
              ('Temperature Setpoint', [0, 60]),
              ('Slew_1', [200, 400, 800, 1600, 3200, 6400])]
params = ['sn', 'tp', 'v1', 'temp', 'slew']
代码:

过滤器的格式为字符串:

filter_t: (self.df["Test Points"] == 3P3V) & (self.df["Slew_1"] == 5000)
filter_t: (self.df["Serial Number"] == 2450) & (self.df["Test Points"] == 3P3V) & (self.df["Voltage_1"] == 11.6) & (self.df["Temperature Setpoint"] == 25.0) & (self.df["Slew_1"] == 5000)
过滤器的格式:

filter_l: [('Test Points', '3P3V_Edge'), ('Slew_1', 200)]
filter_l: [('Serial Number', 1234), ('Test Points', '3P3V'), ('Voltage_1', 11.6), ('Temperature Setpoint', 25.0), ('Slew_1', 200)]
解决方案1-无问题工作:

filtered_df = {1: df}
for x in filter_l:
     filtered_df[1] = (filtered_df[1].loc[(filtered_df[1][f'{x[0]}'] == x[1])])
解决方案2-可能吗

df_filter = self.df.loc[filter_t]
过滤器示例:

df_filter = self.df.loc[(self.df['Serial Number'] == 1234) &
                        (self.df['Test Points'] == '3P3V') &
                        (self.df['Voltage_1'] == 11.6) &
                        (self.df['Temperature Setpoint'] == 25.0) &
                        (self.df['Slew'] == 200)]
最终解决方案@John Zwinck-Thx:

filter_t = ' & '.join(f'{c[0]} == "{b}"' for b, c in zip(i, test_tuple))
filtered_df = df.loc[df.eval(filter_t)]

print(f'filter_t: {filter_t}')
>>> filter_t: Test_Points == "3P3V" & Slew_1 == "5000"

如果您可以安装
numexpr
,我建议您尝试一下

首先,您需要在列名中使用下划线(或不使用下划线)替换空格。然后,构建并使用如下筛选器字符串:

filter_str = 'Serial_Number == 2450 and Test_Points == "3P3V" and Voltage_1 == 11.6'
df_filter = df.query(filter_str)
如果安装了
numexpr
,这可能是最快的解决方案

或者,创建一个掩码列表,然后将它们合并。这与您的解决方案1类似,但比其更好:

masks = [df[x[0]] == x[1] for x in filter_l] # list of bool arrays
filtered_df = df[np.logical_and.reduce(masks)] # combine and apply masks
masks = [df[x[0]] == x[1] for x in filter_l] # list of bool arrays
filtered_df = df[np.logical_and.reduce(masks)] # combine and apply masks