1

Is it possible to get unique values from multiple columns? I would like each column to have it's own unique values output via a loop.

df["col A"].unique()
Col A Col B
3 312
4 6456
3 4
1 4

Output

Col A: 3, 4, 1

Col B: 6456, 312, 4

2
  • Based of the example data you gave us, could you tell us what is the expected output? This way we can understand your specific problem, since it seems that .unique() does exactly what you want. Commented Dec 22, 2020 at 17:00
  • 1
    If you want some kind of vectorized implementation of unique to all the columns of the df at once you could use df.apply(lambda col: col.unique()) but I'm not sure if it will work because each columns probably will have an output of different length Commented Dec 22, 2020 at 17:04

3 Answers 3

2

It kinda depends on what you want as output. You can do it like this to get a dict with each column:

unique_vals = {col:df[col].unique() for col in df}

But you probably don't want it as a dataframe like this, because there is no guarantee the amount of unique values is the same for each column.

If you only want to print it it's as simple as:

for col in df:
    print(f'{col}: {df[col].unique()}')
Sign up to request clarification or add additional context in comments.

Comments

2

Try using list comprehension:

cola, colb = [df[i].unique() for i in df]

or

df.apply(lambda x: x.unique())

Comments

1

Let us try

out = pd.DataFrame(map(set,df.values.T.tolist())).T
Out[161]: 
   0     1
0  1   312
1  3  6456
2  4     4

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.