齿隙误差. pandas 使用动态查询字符串过滤数据帧. [英] Backlash error. Pandas filter dataframe using dynamic query string.
问题描述
大家好, 该问题与Python反冲错误有关. 我正在创建用于过滤大熊猫的动态查询字符串. 代码是:
Hi all, The problem is related to the Python backlash error. I am creating a dynamic query string for filtering in pandas. The code is:
filters = dict(wlbWellType=['EXPLORATION'])
query_string = ''
index = 0
for (k,v) in filters.iteritems():
for i in v:
if (index == 0):
query_string += '"{}"'.format((k) + ' == '+"'{}'".format(i))
else:
query_string += ' & ' '"{}"'.format((k) + ' == ' +
"'{}'".format(i))
index += 1
如果我执行"print query_string",则输出为
If I do "print query_string" the output I got is
"wlbWellType == 'EXPLORATION'"
如果我执行"query_string",则输出为
If I do "query_string" the output I got is
'"wlbWellType == \'EXPLORATION\'"'
我想要
"wlbWellType == 'EXPLORATION'"
作为输出而不使用print语句.似乎存在与反冲有关的错误.
as the output without using print statement. Seems there is an error related to backlash.
query_string输出将用作:
The query_string output will then be used as:
df.query(query_string)
有人可以帮我解决上述问题吗?
Can anyone please help me with the above problem?
预先感谢
推荐答案
您可以编写自己的帮助器函数(类似于您现在正在尝试的功能,但使用了**kwargs
)并使用@varname
语法作为价值占位符.
You can write your own helper function (similar to what you're trying now but making use of **kwargs
) and use the @varname
syntax as the value placeholder.
def my_filter(df, **kwargs):
qs = ' & '.join('{0} == @{0}'.format(k) for k in kwargs)
return df.query(qs, local_dict=kwargs)
然后使用如下:
new_df = my_filter(df, wlbWellType='EXPLORATION', otherColumn='SOMETHING')
此方法比手动转义值更安全,因为@varname
语法会根据值的类型为您做适当的选择.
This method is safer than manually escaping values as the @varname
syntax will do that appropriately for you depending on the value's type.
这篇关于齿隙误差. pandas 使用动态查询字符串过滤数据帧.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!