Running query in Pandas against a column that starts with a number (Python 3)

292 views Asked by At

I'm trying to run a query on a data frame where I'm evaluating the value of a column that starts with a number. Something like this:

df.query('1A_col == "1000"')

I've read a fair few posts about how to deal with this and the general solution is to use backticks around the column name (as it starts with a number, and this will be run through eval() and in Python you can't have a var name starting with a number etc). So it should look like this:

df.query('`1A_col` == "1000"')

But this only seems to work in versions of Python prior to 3. Since 3 the backtick is no longer supported. If i run this in 3 I get something along the lines of 'BACKTICK_QUOTED_STRING_1A_col' is not defined.

So, my question is - is there a way to achieve this in Python 3? If so how? Also, I need to use query() as in my solution I'm allowing the user to pass in the filter string into my program and effectively executing their query for them.

The data frame would look like this:

data = {'AB': [1,2,3], '1A_col': [1000,2,3]}
df = pd.DataFrame(data)
1

There are 1 answers

6
Mayank Porwal On BEST ANSWER

I am using Python 3 and Pandas version 1.1:

In [1666]: sys.version
Out[1666]: '3.7.3 (default, Apr 24 2020, 18:51:23) \n[Clang 11.0.3 (clang-1103.0.32.62)]'

In [1663]: pd.__version__
Out[1663]: '1.1.0'

In [1664]: df.query('`1A_col` == 1000')
Out[1664]: 
   AB  1A_col
0   1    1000