0

I am trying to count uniqiue values that are in few columns. My data frame looks like that:

Name      Name.1    Name.2    Name.3
x         z          c          y
y         p          q          x
q         p           a         y

Output should looks like below:

x   2
z   1
c   1
y   3
q   2
p   2
a   1

I used a groupby or count_values but couldn't get a correct output. Any ideas ? Thanks All !

2

2 Answers 2

2

Seems you want to consider values regardless of their row or column location. In that case you should collapse the dataframe and just use Counter.

from collections import Counter

arr = np.array(df)
count = Counter(arr.reshape(arr.size))
Sign up to request clarification or add additional context in comments.

7 Comments

It work's great. Thanks mate ! Do you have a idea how to show for instance 5 results that occurs the most often ?
sorted(count.items(), key=lambda x: x[1])
It returns empty list
Sorry which one? The sorted result or count?
You can do sorted(..., reverse=True)[:5] to get the top five by occurrence
|
0

Another (Pandas-based) approach is to (Series) apply value_counts to multiple columns and then take the sum (column-wise)

df2 = df.apply(pd.Series.value_counts)
print(df2.sum(axis=1).astype(int)
a    1
c    1
p    2
q    2
x    2
y    3
z    1
dtype: int32

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.