Backlash error. Pandas filter dataframe using dynamic query string.

后端 未结 3 1495
轮回少年
轮回少年 2021-01-27 18:50

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:

                


        
3条回答
  •  情话喂你
    2021-01-27 19:02

    The reason is that you are wrapping your key in python format string ( "'{}'".format ) as well. Try this solution:

    query_string = ""
    index = 0
    for (k,v) in filters.iteritems():
      for i in v:
        if (index == 0):
          query_string += str(k) + " == " + "'{}'".format(i)
        else:
          query_string += " & " + str(k) + " == " + "'{}'".format(i)
      index += 1
    

提交回复
热议问题