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?
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.
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().
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With