python dataframe query with spaces in column name

前端 未结 3 681
庸人自扰
庸人自扰 2021-01-29 02:22

I want to filter dataframe using query

ExcludeData= [1,3,4,5]
dfResult.query(\'Column A in @ExcludeData\')

How do I use Column A in query with

相关标签:
3条回答
  • 2021-01-29 02:44

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

    dfResult = dfResult[dfResult['Column A'].isin(ExcludeData)]
    
    0 讨论(0)
  • 2021-01-29 02:47

    As pointed out by @ayhan, it's not supported right now. However, you can make sure to read your columns without space.

    In [51]: df
    Out[51]: 
        A  B
    0   1  2
    1   3  5
    2   4  8
    3   5  5
    4   4  4
    5   5  2
    6   9  8
    7   8  9
    8   4  6
    9   2  3
    
    In [52]: df.columns
    Out[52]: Index([u' A', u'B'], dtype='object')
    
    In [53]: pd.read_csv(pd.io.common.StringIO(df.to_csv(index=False)),sep='\s*,').query('A in [2,3]') 
    Out[53]: 
       A  B
    1  3  5
    9  2  3
    
    0 讨论(0)
  • 2021-01-29 02:52

    Starting with Pandas v. 0.25, it is possible to refer to columns with names containing spaces if you enclose the column name in backticks within the query.

    Using Pandas 0.25.2:

    >>> df = pd.DataFrame({'a a': [1, 0], 'b b': [1, 1]})
    >>> df
       a a  b b
    0    1    1
    1    0    1
    >>> df.query('`a a`==`b b`')
       a a  b b
    0    1    1
    

    From the API docs: https://pandas.pydata.org/pandas-docs/version/0.25/reference/api/pandas.DataFrame.query.html

    In your case, the usage would be:

    dfResult.query('`Column A` in @ExcludeData')
    
    0 讨论(0)
提交回复
热议问题