SyntaxError when accessing column named “class” in pandas DataFrame

╄→гoц情女王★ 提交于 2019-12-01 21:13:36

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).

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

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