List unique values in a Pandas dataframe
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]
You can using set
list(map(set,df.values.T))
Out[978]:
[{'Coch', 'Mari', 'Pima', 'Santa', 'Yuma'},
{'Amy', 'Jake', 'Jason', 'Molly', 'Tina'},
{2012, 2013, 2014}]
After put into Series
pd.Series(list(map(set,df.values.T)),index=df.columns)
Out[980]:
name {Santa, Pima, Yuma, Coch, Mari}
report {Jason, Amy, Jake, Tina, Molly}
year {2012, 2013, 2014}
dtype: object
If you would like to have you results in a list you can do something like this
[df[col_name].unique() for col_name in df.columns]
out:
[array(['Coch', 'Pima', 'Santa', 'Mari', 'Yuma'], dtype=object),
array(['Jason', 'Molly', 'Tina', 'Jake', 'Amy'], dtype=object),
array([2012, 2013, 2014])]
This will create a 2D list of array, where every row is a unique array of values in each column.
If you would like a 2D list of lists, you can modify the above to
[df[i].unique().tolist() for i in df.columns]
out:
[['Coch', 'Pima', 'Santa', 'Mari', 'Yuma'],
['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
[2012, 2013, 2014]]