1

My dataset has this structure

A = [A1, A2, A3, A4]
B = [B1, B2, B3]
C = [C1, C2, C3, C4, C5]

I want to count the occurrences of all variables in my dataset, such as:

A1    3
A2    2
A3    1
...
C4    4
C5    5

I have tried

df.groupby(df.columns[0]).A.count()

but it only works column by column, is there any way to count occurrences for the whole dataset at once? Thank you.

Thanh Nguyen
  • 902
  • 2
  • 12
  • 31

3 Answers3

2

You can use stack the value_counts

df.stack().value_counts()
Out[1298]: 
4    2
3    2
2    2
5    1
1    1
dtype: int64

Data input

df=pd.DataFrame({'A':[1,2,3,4],'B':[2,3,4,5]})
BENY
  • 317,841
  • 20
  • 164
  • 234
2
pd.value_counts(df.values.ravel())
A.Kot
  • 7,615
  • 2
  • 22
  • 24
1

You can use apply with count on axis = 1

>>> df=pd.DataFrame({'A':[1,2,3,4],'B':[2,3,4,5]})
>>> df.apply(pd.value_counts).count(axis=1)

Outputs

1    1
2    2
3    2
4    2
5    1
dtype: int64
Sohaib Farooqi
  • 5,457
  • 4
  • 31
  • 43