Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print unique values in pandas data frame

Tags:

python

pandas

import pandas as pd    
df = pd.DataFrame({'a':[1,2,3,4],'b':['a','b','d','d'],'c':['v','v','g','w']})
print(df.apply(lambda x: x.unique().shape[0]))

above code will print count of unique values in each columns. I want to print count of unique values only for columns of 'object' type.

is there any way to filter only 'object' columns

like image 476
Gowtham M Avatar asked Dec 24 '22 19:12

Gowtham M


1 Answers

You can use select_dtypes() as @JulianCienfuegos has already said in conjunction with nunique():

In [9]: df.select_dtypes(include=['object']).apply(lambda x: x.nunique())
Out[9]:
b    3
c    3
dtype: int64

As @root has added in the comment starting with Pandas 0.20.0 it should be possible to use DataFrame.nunique():

df.select_dtypes(include=['object']).nunique()
like image 56
MaxU - stop WAR against UA Avatar answered Jan 08 '23 09:01

MaxU - stop WAR against UA