问题
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