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,1], [5,6,7,8], [9,10,11,1]])
for i in range(0,len(x)):
if x[i][4]==0:
x=np.delete(x, i,0)
I get the following error:
Traceback (most recent call last):
File "", line 2, in
if x[i][4]==0:
IndexError: index out of bounds
You're trying to reference the fourth column with [4], but since it's zero based it's actually [3]
You can use indexing:
>>> x[x[:,3] != 1]
array([[ 1, 2, 3, 0],
[11, 2, 3, 24],
[ 5, 6, 7, 8]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With