Pandas query function not working with spaces in column names

It is not possible yet. Check github issue #6508:

Note that in reality .query is just a nice-to-have interface, in fact it has very specific guarantees, meaning its meant to parse like a query language, and not a fully general interface.

Reason is for query need string to be a valid python expression, so column names must be valid python identifiers.

Solution is boolean indexing:

df = df[df['a b'] == 5]

Pandas 0.25+

As described here:

DataFrame.query() and DataFrame.eval() now supports quoting column names with backticks to refer to names with spaces (GH6508)

So you can use:

a.query('`a b`==5')

Pandas pre-0.25

You cannot use pd.DataFrame.query if you have whitespace in your column name. Consider what would happen if you had columns named a, b and a b; there would be ambiguity as to what you require.

Instead, you can use pd.DataFrame.loc:

df = df.loc[df['a b'] == 5]

Since you are only filtering rows, you can omit .loc accessor altogether:

df = df[df['a b'] == 5]

From pandas 0.25 onward you will be able to escape column names with backticks so you can do

a.query('`a b` == 5')