1

I have data having over 385 features, to find uniques for a column i have used df.unique() function. However I have to find unique values over all 385 columns.

I tried using for loop as under,

col = [df_train.columns]

for i in col:
    print(i.unique())

I'm getting an output as under

Index(['ID', 'y', 'X0', 'X1', 'X2', 'X3', 'X4', 'X5', 'X6', 'X8',
       ...
       'X375', 'X376', 'X377', 'X378', 'X379', 'X380', 'X382', 'X383', 'X384',
       'X385'],
      dtype='object', length=366)

However the above are the columns names of the dataset and not the unique values of each columns.

I'm doing a concept error when I'm applying for loop, it would be appreciated to correct me where I'm going wrong or an alternative method to do the same.

Thanks in advance.

3 Answers 3

1
for i in df_train.columns:
    print(df_train[i].unique())

If you do

print(df_train.columns)

It will give you only column names

eg. ["x0","x1","x2"] etc

You need to use indexing to access column values like df_train["column_name"]

Sign up to request clarification or add additional context in comments.

1 Comment

If you have time, can you please explain where did I go wrong in my above code? So that I can avoid concept errors going further.
0

to find unique values in a dataframe, we can use describe method as follows

df.describe().loc['unique']

Comments

0

Try to run the unique() method per column:

col = df.columns.values.tolist()
unique_vals_list = []

for column in col:
    unique_vals_list.append(list(df[column].unique()))

Your unique_vals_list will now contain all the unique value per column. This will be a list of list, so per index is actually the same index of your dataframe.

So accessing all unique for your first column will have the syntax:

unique_vals_list[0]

Accessing the 2nd column unique values:

unique_vals_list[1]

And so on.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.