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
Python: How to Match Nested Parentheses With Regex
Compare a Column Between 2 CSV Files and Write Differences Using Python
How to Split a CSV File Row to Columns in Python
Grab a Number After a String in a File
How to Change the Title Bar in Tkinter
Change Date Formats in CSV With Python 3
Concatenate Two Columns in Csv: Python
Pandas.Read_Excel Parameter "Sheet_Name" Not Working
Remove Last Few Characters in Pyspark Dataframe Column
Python: How to Read and Load an Excel File from Aws S3
Remove Very First Row in Pandas
How to Write Multiple Images (Subplots) into One Image
How to Append Data Using Openpyxl Python to Excel File from a Specified Row
How to Read a List of Parquet Files from S3 as a Pandas Dataframe Using Pyarrow
How to Display a Plot in Fullscreen
How to Send Keys to a Game I Am Playing,Using Python