Trying to get a hold of pd.query function
Getting an error
UndefinedVariableError: name ‘BACKTICK_QUOTED_STRING__AT_key’ is not defined
for the below pandas Python code. What did I do 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")
Advertisement
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}")