airbud
airbud

Reputation: 49

Counting all unique values in a data set

I have a data set of 60 plus computers with each column being the computer and the rows being the collection of all the software installed from each PC. I want to be able to count each unique value(software), so I can see how many of each software is currently installed.

data = [['a','a','c'],['a','b','d'],['a','c','c']]
df = pd.DataFrame(data,columns=['col1','col2','col3'])
df


col1  col2  col3
a      a      c
a      b      d
a      c      c

I expect the following output
a 4
b 1
c 3

Upvotes: 0

Views: 31

Answers (1)

BENY
BENY

Reputation: 323296

value_counts after melt

df.melt().value.value_counts()
Out[648]: 
a    4
c    3
b    1
d    1
Name: value, dtype: int64

numpy.unique for speed up

pd.Series(*np.unique(df.values.ravel(),return_counts=True)[::-1])
Out[653]: 
a    4
b    1
c    3
d    1
dtype: int64

Upvotes: 3

Related Questions