0

So I have a dataframe that has 300 columns and thousands of rows. Each column contains a value between 0-30. I am trying, to find the total number of times that the number 8 occurs in each column.

Ideally, I'm trying to create a list of length 300 with each index corresponding to the index of the column, with a value corresponding to the number of rows that contain the number 8 in that column.

Any help or guidance is appreciated, thank you.

  • 1
    Does this answer your question? [Count the frequency that a value occurs in a dataframe column](https://stackoverflow.com/questions/22391433/count-the-frequency-that-a-value-occurs-in-a-dataframe-column) – BcK Mar 17 '21 at 23:19

2 Answers2

2

You can use a boolean test and .sum()

>>> df
   a  b  c  d
0  8  6  7  8
1  8  8  7  6
2  1  2  3  4

>>> df == 8
       a      b      c      d
0   True  False  False   True
1   True   True  False  False
2  False  False  False  False

>>> (df == 8).sum()
a    2
b    1
c    0
d    1
dtype: int64
0

Something like this should help:

df.isin([8]).sum(axis=0)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
codemaster
  • 31
  • 3