Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

counting the number of non-zero numbers in a column of a df in pandas/python

I have a df that looks something like:

a b c d e 0 1 2 3 5 1 4 0 5 2 5 8 9 6 0 4 5 0 0 0

I would like to output the number of numbers in column c that are not zero.

like image 828
user5826447 Avatar asked Jan 23 '16 20:01

user5826447


2 Answers

Use double sum:

print df
   a  b  c  d  e
0  0  1  2  3  5
1  1  4  0  5  2
2  5  8  9  6  0
3  4  5  0  0  0

print (df != 0).sum(1)
0    4
1    4
2    4
3    2
dtype: int64

print (df != 0).sum(1).sum()
14

If you need count only column c or d:

print (df['c'] != 0).sum()
2

print (df['d'] != 0).sum()
3

EDIT: Solution with numpy.sum:

print ((df != 0).values.sum())
14
like image 95
jezrael Avatar answered Oct 18 '22 09:10

jezrael


Numpy's count_nonzero function is efficient for this.

np.count_nonzero(df["c"])

like image 22
Stig Johan B. Avatar answered Oct 18 '22 10:10

Stig Johan B.