Count non-zeros using numpy.count_nonzero | 数非零数

Today our class practiced making the computer count number of non-zero numbers using the numpy library from Python.  This can be useful if you have a ton of numbers.

import numpy as np; import pandas as pd

some_array = np.array([[0,1,7,0,0],[3,0,0,2,19]])

array([[ 0,  1,  7,  0,  0],
[ 3,  0,  0,  2, 19]])

np.count_nonzero(some_array)

5

np.count_nonzero(some_array,  axis=0)  Count across the rows, i.e. count along the column

array([1, 1, 1, 1, 1], dtype=int64)

np.count_nonzero(some_array,  axis=1)  Count across the columns, i.e. count along the row

array([2, 3], dtype=int64)

We talked about this example:

d = {'Basket1': [3, 0], 'Basket2': [3, 4]}
df = pd.DataFrame(data=d, index=['Apple','Chips'])

# Count the number of non-zeros across the rows
pd.Series(np.count_nonzero(df, axis=0), index=df.columns.tolist())

This was the result we got.

Basket1    1
Basket2    2
dtype: int64

That was a very tiny data. If we have a dataset with a million rows and columns, we should definitely do this!

Leave a Reply

Your email address will not be published.