How do I count the occurrence of a certain item in an ndarray?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In NumPy, counting how many times a value appears in an ndarray is usually a one-line operation. The standard pattern is to build a boolean mask and then count the True values, which is both readable and fast because the work stays inside NumPy's vectorized operations.
Count One Exact Value
The most direct way to count a single item is:
arr == target produces a boolean array of the same shape. Every matching element becomes True. np.count_nonzero then counts those True values.
You will also see:
That works because True behaves like 1 and False behaves like 0, but count_nonzero expresses the intent more clearly.
Count by Row or by Column
Sometimes you want more than a single total. You may need to know how often a value appears in each row or in each column. The same mask can be counted along a chosen axis.
This is useful in matrix analysis, image processing, and data-cleaning tasks where the position of matches matters.
Count All Unique Values at Once
If you need a frequency table for every distinct item, do not loop through targets manually. Use np.unique with return_counts=True.
That approach is usually the best fit for summaries, histograms of categorical data, or quick exploratory checks.
For dense non-negative integers, np.bincount can also be very efficient:
Here counts[2] is the number of times the value 2 appears.
Count Values Matching a Condition
The exact same pattern works for conditions, not just equality. This is one reason the mask approach is so powerful.
You are still counting True values, but now the boolean mask is created from a comparison expression instead of an exact match.
Special Cases: Floating Point Values and NaN
Floating-point arrays need extra care because direct equality is sometimes too strict. Two values that look identical when printed may differ slightly in binary representation.
Use np.isclose when approximate equality is what you actually want:
NaN is another special case because NaN != NaN. That means direct comparison will never count missing values correctly.
Whenever the target is missing data, use np.isnan rather than == np.nan.
Common Pitfalls
- Writing a Python loop over every element instead of using a vectorized mask.
- Forgetting the
axisargument and getting one global total instead of row-wise or column-wise counts. - Using direct equality for floating-point values that should be compared approximately.
- Trying to count
NaNwith== np.naninstead ofnp.isnan. - Using
np.bincounton negative values or non-integer data, where it does not apply.
Summary
- Use
np.count_nonzero(arr == target)to count one exact value in anndarray. - Add
axis=0oraxis=1when you need counts by column or by row. - Use
np.unique(..., return_counts=True)for a full frequency table. - Use
np.isclosefor approximate floating-point comparisons. - Use
np.isnanwhen the item you need to count is missing-value data.

