9

I'm trying to write a function to delete all rows in which have a zero value in. This is not from my code, but an example of the idea I am using:

import numpy as np
a=np.array(([7,1,2,8],[4,0,3,2],[5,8,3,6],[4,3,2,0]))
b=[]

for i in range(len(a)):
    for j in range (len(a[i])):
        if a[i][j]==0:
            b.append(i)

print 'b=', b
for zero_row in b:
    x=np.delete(a,zero_row, 0)

print 'a=',a

and this is my output:

b= [1, 3]
a= [[7 1 2 8]
 [4 0 3 2]
 [5 8 3 6]
 [4 3 2 0]]

How do I get rid of the rows with the index in b? Sorry, I'm fairly new to this any help would be really appreciated.

1
  • Just one comment. That piece of example code you posted loops over b to delete individual rows. That's not needed at all. a = np.delete(a, b, axis=0) works like a charm (axis specified to make it more evident what we're doing) Feb 6, 2014 at 11:24

4 Answers 4

16

I'm trying to write a function to delete all rows in which have a zero value in.

You don't need to write a function for that, it can be done in a single expression:

>>> a[np.all(a != 0, axis=1)]
array([[7, 1, 2, 8],
       [5, 8, 3, 6]])

Read as: select from a all rows that are entirely non-zero.

4
  • is there a way to do this for a 1D array? would you just leave out the axis specification? i.e. b[np.all(b != 0, )] where b is a 1d array Aug 23, 2013 at 9:39
  • 2
    b = b[b!=0] removes the 0's in b. It is called boolean indexing. Aug 23, 2013 at 11:33
  • What if one wants to remove all columns containing "2"? a[np.all(a !=2, axis=0)] does not work as expected. I had to resort to manipulating the transpose instead. Any suggestion? Apr 10, 2014 at 13:51
  • @user1211129 I think transposing and later transposing back is the sensible thing to do.
    – Fred Foo
    Apr 10, 2014 at 14:15
3

Looks like np.delete does't change the array, just returns a new array, so

Instead of

x = np.delete(a,zero_row, 0)

try

a = np.delete(a,zero_row, 0)
1
  • ah that was stupid, probably to early in the morning. However I did change this and got ValueError:invalid entry thanks for the quick response Aug 23, 2013 at 8:12
1

I think I have found the answer:

as @tuxcanfly said I changed x to a. Also I have now removed the for loop as it removed the row with index 2 for some reason.

Instead I now just chose to delete the rows using b as the delete function with use the elements in the list to remove the row with that index.

the new code:

import numpy as np
a=np.array(([7,1,2,8],[4,0,3,2],[5,8,3,6],[4,3,2,0]))
b=[]

for i in range(len(a)):
    for j in range (len(a[i])):
        if a[i][j]==0:
            b.append(i)
print 'b=',b
for zero_row in b:
    a=np.delete(a,b, 0)

print 'a=',a

and the output:

b= [1, 3]
a= [[7 1 2 8]
 [5 8 3 6]]
1

I think this helps readability (and allows you to loop once, not twice):

#!/usr/bin/env python

import numpy as np
a = np.array(([7,1,2,8], [4,0,3,2], [5,8,3,6], [4,3,2,0]))
b = None

for row in a:
    if 0 not in row:
        b = np.vstack((b, row)) if b is not None else row

a = b
print 'a = ', a

In this version, you loop over each row and test for 0's membership in the row. If the row does not contain a zero, you attempt to use np.vstack to append the row to an array called b. If b has not yet been assigned to, it is initialized to the current row.

Not the answer you're looking for? Browse other questions tagged or ask your own question.