Python Dataframe Query With Spaces in Column Name

Pandas query function not working with spaces in column names

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

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

python dataframe query with spaces in column name

I wouldn't use query function. I would use the square bracket notation:

dfResult = dfResult[dfResult['Column A'].isin(ExcludeData)]

Querying Pandas DataFrame with column name that contains a space or using the drop method with a column name that contains a space

If I understood correctly your issue, maybe you can just apply a filter like:

df = df[df['Sale Item'] != 'item1']

which returns:

         Date    price Sale Item
1 2012-06-12 1610.02 item2
2 2012-06-13 1618.07 item3
3 2012-06-14 1624.40 item4
4 2012-06-15 1626.15 item5
5 2012-06-16 1626.15 item6
6 2012-06-17 1626.15 item7

Pandas column access w/column names containing spaces

I think the default way is to use the bracket method instead of the dot notation.

import pandas as pd

df1 = pd.DataFrame({
'key': ['b', 'b', 'a', 'c', 'a', 'a', 'b'],
'dat a1': range(7)
})

df1['dat a1']

The other methods, like exposing it as an attribute are more for convenience.

Using pandas to query a column with a multi word title

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.

How to Reference a Pandas Column that has a dot in the name

Using the replace command to remove offensive characters works ok: Removing space from dataframe columns in pandas.



Related Topics



Leave a reply



Submit