18

Passing on new application of information I learned that was part of another question: Unable to query a local variable in pandas 0.14.0

Credit and thanks to user @choldgraf. I'm applying his answer from the above link differently.

Objective: To use a variable as the column name in a query

Failed examples:

import pandas as pd
fooframe = pd.DataFrame({'Size':['Large', 'Medium', 'Small', 'Tiny'], 'Color':[1, 2, 3, 4]})
myvar = 'Size'
subframe = fooframe.query("myvar == 'Large'")

The code above returns a key error for 'myvar'.

import pandas as pd
fooframe = pd.DataFrame({'Size':['Large', 'Medium', 'Small', 'Tiny'], 'Color':[1, 2, 3, 4]})
myvar = 'Size'
subframe = fooframe.query("@myvar == 'Large'")

The code above adds "@" before myvar in the query to reference myvar as a local variable. However, the code still returns an error.

3
  • 1
    Is this a bug with pandas, or intended to not work? As of March 2019, I also cannot reference to columns by variables... Commented Mar 29, 2019 at 13:04
  • Hi @Thomas, did you see my answer below? Is that still not working for you? The question demonstrates two techniques that I couldn't get to work. Commented Apr 4, 2019 at 18:27
  • 1
    Hi @TempleGuard527, I saw the answer and upvoted it because it works, but was wondering whether any future visitors to this question find a way to make it work with "@variable" instead since this is the intended (and less cluttered) method Commented Apr 5, 2019 at 11:56

2 Answers 2

25

Credit and thanks to user @choldgraf. I used the technique he mentioned in another post (Unable to query a local variable in pandas 0.14.0) not for the value in the column but for the column name.

A variable can be used as the column name in a pandas query by inserting it into the query string like so:

import pandas as pd
fooframe = pd.DataFrame({'Size':['Large', 'Medium', 'Small', 'Tiny'], 'Color':[1, 2, 3, 4]})
myvar = 'Size'
subframe = fooframe.query("`{0}` == 'Large'".format(myvar))

(Where backticks are used to bracket the column name, dealing with special characters and spaces in column names.)

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

2 Comments

The answer is perfect. It will only create an issue if the column name has space in it, you can use backtick to overcome subframe = fooframe.query("{0} == 'Large'".format(myvar))
fooframe.query(f"{myvar} == 'Large' ") is also possible.
4

@TempleGuard527's answer works perfectly. This answer is just an addition with the introduction of f-strings from python3.6. Now, you can do something like this:

import pandas as pd
fooframe = pd.DataFrame({'Size':['Large', 'Medium', 'Small', 'Tiny'], 'Color':[1, 2, 3, 4]})
myvar = 'Size'
subframe = fooframe.query(f"`{myvar}` == 'Large'")

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.