Pandas query function not working with spaces in column names

前端 未结 5 941
-上瘾入骨i
-上瘾入骨i 2020-11-30 10:45

I have a dataframe with spaces in column names. I am trying to use query method to get the results. It is working fine with \'c\' column but getting error for \

5条回答
  •  遥遥无期
    2020-11-30 11:49

    It is not possible yet. Check github issue #6508:

    Note that in reality .query is just a nice-to-have interface, in fact it has very specific guarantees, meaning its meant to parse like a query language, and not a fully general interface.

    Reason is for query need string to be a valid python expression, so column names must be valid python identifiers.

    Solution is boolean indexing:

    df = df[df['a b'] == 5]
    

提交回复
热议问题