1

I'm trying to create a function that takes the input name of a value in a column and that value will then be used in a df.query function. However, I cannot figure out how to make it a variable that it recognizes as the input.

This is what I have right now:

def gettingWeeks(stateAbbr, stateName):

    stateCases = cases.query('state == stateName')

But it does not recognize stateName. Is there a way to do this? Thanks!

1 Answer 1

3

Pandas DataFrame.query method expects an expression string created accordingly to its specific syntax. To use variables from the current name space you have to use @ symbol before the name of the variable:

stateCases = cases.query("state == @stateName")

Should work fine.

Here is the doc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.