Reputation: 51
I am trying to remove the rows which have their mean equal to 0. I have tried this:
import numpy as np
a = np.zeros((4,4))
for i in range(len(a)):
if (i%2)==0 : a[i]= np.arange(4*i,4*(i+1))
array([[ 0., 1., 2., 3.],
[ 0., 0., 0., 0.],
[ 8., 9., 10., 11.],
[ 0., 0., 0., 0.]])
np.ma.MaskedArray(a, mask=(np.mean(a,0)==0))
np.ma.compress_rows(a)
I would like to get this:
a =([[ 0., 1., 2., 3.],
[ 8., 9., 10., 11.]])
Upvotes: 1
Views: 487
Reputation: 76997
Using index and conditions
In [187]: a[~(a.mean(axis=1)==0)]
Out[187]:
array([[ 0., 1., 2., 3.],
[ 8., 9., 10., 11.]])
Upvotes: 4