‘BACKTICK_QUOTED_STRING__AT_key’ is not defined for Pandas Quering function

Tags: , , , ,



Trying to get a hold of pd.query function

Getting a UndefinedVariableError: name ‘BACKTICK_QUOTED_STRING__AT_key’ is not defined for the below pandas python code. Where am i going wrong?

def ft_equal(df,key,value):
    # print(key)
    # print(value)
    # return df[df[key] == value]
    # print(key)
    # print(df[key])
    # return df.query('@key == "%s"' %value)
    return df.query('`@key` == @value')

pd.DataFrame.ft_equal = ft_equal

df.ft_equal("Current State","Disburse")

Answer

Try this :

def ft_equal(df,key,value):
    my_query = key + " == " + value
    return df.query(my_query)

or

def ft_equal(df,key,value):
    return df.query(f"{key} == {value}")


Source: stackoverflow