python - print the unique values in every column in a pandas dataframe -
i have dataframe (df) , want print unique values each column in dataframe.
i need substitute variable (i) [column name] print statement
column_list = df.columns.values.tolist() column_name in column_list:     print(df."[column_name]".unique()   update
when use this: "unexpected eof parsing" no details.
column_list = sorted_data.columns.values.tolist() column_name in column_list:       print(sorted_data[column_name].unique()   what difference between syntax ys-l (above) , below:
for column_name in sorted_data:       print(column_name)       s = sorted_data[column_name].unique()       in s:         print(str(i))      
it can written more concisely this:
for col in df:     print df[col].unique()   generally, can access column of dataframe through indexing using [] operator (e.g. df['col']), or through attribute (e.g. df.col).
attribute accessing makes code bit more concise when target column name known beforehand, has several caveats -- example, not work when column name not valid python identifier (e.g. df.123), or clashes built-in dataframe attribute (e.g. df.index). on other hand, [] notation should work.
Comments
Post a Comment