Pandas: How to Query a Column name with Spaces

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
3 min

banner

# Table of Contents

  1. Pandas: How to Query a Column name with Spaces
  2. In older Pandas versions, you had to use DataFrame.loc

# Pandas: How to Query a Column name with Spaces

You can escape column names that contain spaces by wrapping them in backticks in a Pandas query() call.

The backtick character is right above the Tab key (to the left of the number 1 key).

main.py
import pandas df = pandas.DataFrame({ 'first name': ['Alice', 'Bobby', 'Carl', 'Alice'], 'age': [30, 40, 50, 60], 'net salary': [75, 50, 100, 150] }) # ✅ With String values result = df.query("`first name` == 'Alice'") # first name age net salary # 0 Alice 30 75 # 3 Alice 60 150 print(result) # ✅ Or with Integer Values result = df.query("`net salary` > 50") # first name age net salary # 0 Alice 30 75 # 2 Carl 50 100 # 3 Alice 60 150 print(result)

pandas query column name with spaces

The code for this article is available on GitHub

Notice that we wrapped the column name that contains spaces in backticks ``.

main.py
result = df.query("`first name` == 'Alice'")

The backtick character is right above the Tab key on your keyboard.

The DataFrame.query() method enables us to query the columns of the DataFrame with a boolean condition.

The example above selects the rows that have a first name value of "Alice".

We wrapped the value in single quotes because it is a string.

main.py
result = df.query("`first name` == 'Alice'")

Make sure to alternate between single and double quotes as shown in the code sample.

The whole query is wrapped in double quotes, so we used single quotes when wrapping the string value.

If the value is an integer, don't wrap it in quotes.

main.py
import pandas df = pandas.DataFrame({ 'first name': ['Alice', 'Bobby', 'Carl', 'Alice'], 'age': [30, 40, 50, 60], 'net salary': [75, 50, 100, 150] }) result = df.query("`net salary` > 50") # first name age net salary # 0 Alice 30 75 # 2 Carl 50 100 # 3 Alice 60 150 print(result)

dont wrap integer value in quotes

The code for this article is available on GitHub

Starting with Pandas version 0.25, the DataFrame.query() and DataFrame.eval() methods support quoting column names that contain spaces in backticks.

# In older Pandas versions, you had to use DataFrame.loc

In older Pandas versions, you had to use the DataFrame.loc label indexer because query() couldn't handle column names with spaces.

main.py
import pandas df = pandas.DataFrame({ 'first name': ['Alice', 'Bobby', 'Carl', 'Alice'], 'age': [30, 40, 50, 60], 'net salary': [75, 50, 100, 150] }) result = df.loc[df['net salary'] > 50] # first name age net salary # 0 Alice 30 75 # 2 Carl 50 100 # 3 Alice 60 150 print(result)

using dataframe loc in older pandas versions

The code for this article is available on GitHub

Make sure to use bracket notation and quote the column name as shown in the code sample.

We could shorten this a little by removing the .loc indexer to achieve the same result.

main.py
import pandas df = pandas.DataFrame({ 'first name': ['Alice', 'Bobby', 'Carl', 'Alice'], 'age': [30, 40, 50, 60], 'net salary': [75, 50, 100, 150] }) result = df[df['net salary'] > 50] # first name age net salary # 0 Alice 30 75 # 2 Carl 50 100 # 3 Alice 60 150 print(result)

removing the loc indexer

The code for this article is available on GitHub

The example uses boolean indexing instead of the query() method.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.