Skip to content Skip to sidebar Skip to footer

Index Error, Delete Row From Array If Column Has A Value

I have a array 'x' with four columns. For each row if the 4th column has a value of 1 then I want to delete that entire row: x = np.array([[1,2,3,0],[11,2,3,24],[1,22,3,1],[1,22,3,

Solution 1:

You're trying to reference the fourth column with [4], but since it's zero based it's actually [3]

Solution 2:

You can use indexing:

>>> x[x[:,3] != 1]
array([[ 1,  2,  3,  0],
       [11,  2,  3, 24],
       [ 5,  6,  7,  8]])

Solution 3:

The index of a list starts from 0. So, since there are 4 elements, the indexes are :0,1,2,3. So, if you have to check the 4th element, use index 3.

if x[i][3]==0:
     pass

This will work

Post a Comment for "Index Error, Delete Row From Array If Column Has A Value"