1

Hi I want to pass a variable to pandas query function, however when I do this I get an error. Any idea what I am doing incorrectly?

for example

df = pd.DataFrame({"model" : ["svm"], "rmse" : [0.36], "mae" : [0.28]})
df.query('model == "svm"')[
# this works fine but when I do this,
model_name = "svm"
df.query(f'model == {model_name}')

# >>> UndefinedVariableError: name 'svm' is not defined
# i have also tries with `.format()` method which gives the same error

1
  • 1
    quote your formatted string. df.query(f'model == "{model_name}"') Commented Aug 10, 2022 at 7:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.