Last updated: Apr 12, 2024
Reading time·3 min
DataFrame.loc
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).
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)
Notice that we wrapped the column name that contains spaces in backticks ``.
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.
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.
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)
Starting with Pandas version 0.25, the DataFrame.query()
and
DataFrame.eval()
methods support quoting column names that contain spaces in
backticks.
DataFrame.loc
In older Pandas versions, you had to use the
DataFrame.loc label indexer
because query()
couldn't handle column names with spaces.
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)
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.
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)
The example uses boolean indexing instead of the query()
method.
You can learn more about the related topics by checking out the following tutorials: