It can be written more concisely like this:
for col in df:
print(df[col].unique())
Generally, you can access a column of the DataFrame through indexing using the []
operator (e.g. df['col']
), or through attribute (e.g. df.col
).
Attribute accessing makes the code a bit more concise when the target column name is known beforehand, but has several caveats -- for example, it does not work when the column name is not a valid Python identifier (e.g. df.123
), or clashes with the built-in DataFrame attribute (e.g. df.index
). On the other hand, the []
notation should always work.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…