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.
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
Numpy's count_nonzero
function is efficient for this.
np.count_nonzero(df["c"])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With