Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find unique values for all the columns of a dataframe

How can i get the unique values of all the column in a dataframe ? I am trying to do something like below as of now.

for col in train_features_df.columns:
    print(train_features_df.col.unique())

But this gives me the error AttributeError: 'DataFrame' object has no attribute 'col'

For e.g for below dataframe i want to the below output

 df = pd.DataFrame({'A':[1,1,3],
               'B':[4,5,6],
               'C':[7,7,7]})

I want a output of 1,3 for A and 4,5,6 for B and 7 for C .

like image 414
Invictus Avatar asked Sep 03 '25 07:09

Invictus


1 Answers

You can apply unique on each series by transposing like,

>>> df
   A  B  C
0  1  4  7
1  1  5  7
2  3  6  7
>>> df.T.apply(lambda x: x.unique(), axis=1)
A       [1, 3]
B    [4, 5, 6]
C          [7]
dtype: object
>>> 
like image 196
han solo Avatar answered Sep 04 '25 21:09

han solo