# How to count the occurrence of certain item in an ndarray?

Posted on

### Question :

How to count the occurrence of certain item in an ndarray?

In Python, I have an ndarray `y`
that is printed as `array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])`

I’m trying to count how many `0`s and how many `1`s are there in this array.

But when I type `y.count(0)` or `y.count(1)`, it says

`numpy.ndarray` object has no attribute `count`

What should I do?

``````a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])
unique, counts = numpy.unique(a, return_counts=True)
dict(zip(unique, counts))

# {0: 7, 1: 4, 2: 1, 3: 2, 4: 1}
``````

Non-numpy way:

``````import collections, numpy
a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])
collections.Counter(a)

# Counter({0: 7, 1: 4, 3: 2, 2: 1, 4: 1})
``````

What about using `numpy.count_nonzero`, something like

``````>>> import numpy as np
>>> y = np.array([1, 2, 2, 2, 2, 0, 2, 3, 3, 3, 0, 0, 2, 2, 0])

>>> np.count_nonzero(y == 1)
1
>>> np.count_nonzero(y == 2)
7
>>> np.count_nonzero(y == 3)
3
``````

Personally, I’d go for:
`(y == 0).sum()` and `(y == 1).sum()`

E.g.

``````import numpy as np
y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
num_zeros = (y == 0).sum()
num_ones = (y == 1).sum()
``````

For your case you could also look into numpy.bincount

``````In : a = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])

In : np.bincount(a)
Out: array([8, 4])  #count of zeros is at index 0 : 8
#count of ones is at index 1 : 4
``````

Convert your array `y` to list `l` and then do `l.count(1)` and `l.count(0)`

``````>>> y = numpy.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
>>> l = list(y)
>>> l.count(1)
4
>>> l.count(0)
8
``````

``````y = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])
``````

If you know that they are just `0` and `1`:

``````np.sum(y)
``````

gives you the number of ones. `np.sum(1-y)` gives the zeroes.

For slight generality, if you want to count `0` and not zero (but possibly 2 or 3):

``````np.count_nonzero(y)
``````

gives the number of nonzero.

But if you need something more complicated, I don’t think numpy will provide a nice `count` option. In that case, go to collections:

``````import collections
collections.Counter(y)
> Counter({0: 8, 1: 4})
``````

This behaves like a dict

``````collections.Counter(y)
> 8
``````

If you know exactly which number you’re looking for, you can use the following;

``````lst = np.array([1,1,2,3,3,6,6,6,3,2,1])
(lst == 2).sum()
``````

returns how many times 2 is occurred in your array.

Honestly I find it easiest to convert to a pandas Series or DataFrame:

``````import pandas as pd
import numpy as np

df = pd.DataFrame({'data':np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])})
print df['data'].value_counts()
``````

Or this nice one-liner suggested by Robert Muil:

``````pd.Series([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1]).value_counts()
``````