SyntaxError when accessing column named “class” in pandas DataFrame

牧云@^-^@ 提交于 2019-12-02 00:12:50

问题


I have pandas DataFrame named 'dataset' and it contains a column named 'class'

when I execute the following line I get SyntaxError: invalid syntax

print("Unique values in the Class column:", dataset.class.unique())

It works for another column names but not working with 'class'

How to use a keyword as column name in pandas ?


回答1:


class is a keyword in python. A rule of thumb: whenever you're dealing with column names that cannot be used as valid variable names in python, you must use the bracket notation to access: dataset['class'].unique().

There are, of course, exceptions here, but they work against your favour. For example, min/max is a valid variable name in python (even though it shadows builtins). In the case of pandas, however, you cannot refer to such a named column using the Attribute Access notation. There are more such exceptions, they're enumerated in the documentation.

A good place to begin with further reading is the documentation on Attribute Access (specifically, the red Warning box).




回答2:


class is reserved word. You can do as dataset['class'].unique()



来源:https://stackoverflow.com/questions/49138254/syntaxerror-when-accessing-column-named-class-in-pandas-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!