I have a numpy array like below. I need a count of rows where the first element is 2. So in the array below, four rows start with 2 - the answer would be 4. How is this best accomplished in numpy? (I cannot use pandas, but can use scipy).
array([[1, 4, 5],
[1, 4, 5],
[2, 4, 5],
[2, 4, 5],
[2, 4, 5],
[2, 4, 5],
[3, 4, 5],
[3, 4, 5],
[3, 4, 5],
[3, 4, 5],
[3, 4, 5],
[3, 4, 5]])
First, take the first column, all rows:
a[:,0]
Then, find the 2s:
a[:,0] == 2
That gives you a boolean array. Which you can then sum:
(a[:,0] == 2).sum()
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