Q:

Update numpy array row by condition

I need help) I have NumPy array:
False False False False
False True  True  False
True  True  True  True
False True  True  False
False False False False

How can I get this (take first and last rows, that contains True, and set all of them elements to False)?
False False False False
False False False  False
True  True  True  True
False False False  False
False False False False

A:

arr[arr.any(axis=1).nonzero()[0][[0,-1]]] = False

How it works:
In [19]: arr

Out[19]: 
array([[False, False, False, False],
       [False,  True,  True, False],
       [ True,  True,  True,  True],
       [False,  True,  True, False],
       [False, False, False, False]], dtype=bool)

arr.any(axis=1) finds which rows contains a True value:
In [20]: arr.any(axis=1)
Out[20]: array([False,  True,  True,  True, False], dtype=bool)

nonzero returns a tuple (one item for each axis) of indices of the True rows:
In [21]: arr.any(axis=1).nonzero()
Out[21]: (array([1, 2, 3]),)

We can use indexing to find the index of the first and last row containing a True value:
In [22]: arr.any(axis=1).nonzero()[0][[0,-1]]
Out[22]: array([1, 3])

And finally, we can set those rows to False with 
In [23]: arr[arr.any(axis=1).nonzero()[0][[0,-1]]] = False

In [24]: arr
Out[24]: 
array([[False, False, False, False],
       [False, False, False, False],
       [ True,  True,  True,  True],
       [False, False, False, False],
       [False, False, False, False]], dtype=bool)

