Jasper Bernales - 1 year ago 54

Python Question

I have a numpy array with dim (157,1944).

I want to get all unique indices for each row that has Nonzero element.

example: [[0,0,3,4], [0,0,1,1]] ----> [2,3]

If you look each row, there is a Non Zero element on position [2, 3]

So if I have

[[0,1,3,4], [0,0,1,1]]

i should get [1,2,3] because index 0 has no Nonzero element for all row

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Not sure if your question is completely defined. However, say we start with

```
import numpy as np
a = np.array([[0,0,3,4], [0,0,1,1]])
```

then

```
>>> np.nonzero(np.all(a != 0, axis=0))[0]
array([2, 3])
```

are the indices of the columns for which *none* of the rows are nonzero, and

```
>>> np.nonzero(np.any(a != 0, axis=0))[0]
array([2, 3])
```

are the indices of the columns for which *not all* of the rows are zero (it happens to be the same for the example you gave).

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**