Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List unique values in a Pandas dataframe

I know that

df.name.unique()

will give unique values in ONE column 'name'.

For example:

name    report  year
Coch    Jason   2012
Pima    Molly   2012
Santa   Tina    2013
Mari    Jake    2014
Yuma    Amy     2014

array(['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], dtype=object)

However, let's say I have ~1000 columns and I want to see all columns' unique values all together.

How do I do it?

like image 315
aerin Avatar asked Dec 21 '17 21:12

aerin


People also ask

How do you show unique values in a DataFrame column?

To get the unique values in multiple columns of a dataframe, we can merge the contents of those columns to create a single series object and then can call unique() function on that series object i.e. It returns the count of unique elements in multiple columns.

How do you make a list of unique values from a column of data in Python?

To get unique values from a column in a DataFrame, use the unique(). To count the unique values from a column in a DataFrame, use the nunique().

What does unique () do in pandas?

The unique function in pandas is used to find the unique values from a series. A series is a single column of a data frame. We can use the unique function on any possible set of elements in Python. It can be used on a series of strings, integers, tuples, or mixed elements.


1 Answers

Using a dictionary comprehension with unique:

pd.Series({c: df[c].unique() for c in df})

The resulting output:

name      [Coch, Pima, Santa, Mari, Yuma]
report    [Jason, Molly, Tina, Jake, Amy]
year                   [2012, 2013, 2014]
like image 89
root Avatar answered Sep 30 '22 16:09

root