counting the number of non-zero numbers in a column of a df in pandas/python
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