ocean800 - 6 months ago 59

Python Question

So I have an 2D array

`data`

I want to count the number of rows on a certain condition of the second two columns. For example, in this particular slice of the array, I only have

`1 | 2`

But given the third row is

`range(1,4)`

`range(0,3)`

`1 | 0`

1 | 1

1 | 2

2 | 0

2 | 1

2 | 2

3 | 0

3 | 1

3 | 2

I want to select the rows for which

Any help would be greatly appreciated, thanks!!

Answer

This works:

```
import numpy as np
# data array
data = np.array([[4,3,1,2],[4,3,5,1],[1,2,1,0]])
# array of acceptable combinations
cond = np.array([[1,0],[1,2]])
# index of rows matching the conditions
idx=np.array([row in cond for row in data[:,2:]])
# selected rows
data[idx]
# array([[4, 3, 1, 2],
# [1, 2, 1, 0]]
```

Source (Stackoverflow)