Using pandas to query a column with a multi word title

Viewed 922

If you use this code, you can get all rows with bar in A

import pandas as pd
 df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
                   'B': 'one one two three two two one three'.split()})
 print(df)
print("=============")
print( df.query('A== "bar"') )

WSo when I use this code, I expect to get all rows with bar in A thing but I get an error.

import pandas as pd
 df = pd.DataFrame({'A thing': 'foo bar foo bar foo bar foo foo'.split(),
                   'B thing': 'one one two three two two one three'.split()})
 print(df)
print("=============")
print( df.query('A thing== "bar"') )

is there a way of dealing with spaces in column names?

2 Answers

For pandas 0.25+ you can use backticks:

df.query('`A thing` == "bar"')

Prior versions - you can't do what you want - you'll have to stick to using names that are valid Python literals to use inside query.

You need to use underscores in variable names instead of spaces

df = pd.DataFrame({'A_thing': 'foo bar foo bar foo bar foo foo'.split(),
                   'B_thing': 'one one two three two two one three'.split()})

pandas evaluates it using eval() , a python builtin. so see this for a better and clear understanding.

a='apple' 
apple='ball'
print(eval(a)) // Will give ball as output

BUT

a='app le' 
apple='ball'
print(eval(a)) // ERROR:unexpected EOF while parsing

suppose i write "my name='joker'" instead if "myname='joker'" an error is certain in this case for the first statement. and the syntax is not accepted by python compiler where there are spaces in variable names.

the key dictonary is also a variable so space is not accepted inbetween. if you have more doubts comment to this thread. ill explain further.

Related