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:
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)
Then use as follows:
new_df = my_filter(df, wlbWellType='EXPLORATION', otherColumn='SOMETHING')
This method is safer than manually escaping values as the @varname
syntax will do that appropriately for you depending on the value's type.