指数误差,从阵列中删除行,如果列有一个值 [英] Index error, delete row from array if column has a value
本文介绍了指数误差,从阵列中删除行,如果列有一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组'X'四列。
有关的每一行,如果第4列具有1的值,则欲删除整个行:
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]])
对于i在范围(0,LEN(X)):
如果x [I] [4] == 0:
X = np.delete(X,I,0)
我收到以下错误:
回溯(最近通话最后一个):结果
文件,2号线,在结果
如果x [I] [4] == 0:搜索
IndexError:索引越界
块引用>解决方案您可以使用的索引:
>>> ×〔X [:,3] = 1]
阵列([[1,2,3,0],
[11,2,3,24],
[5,6,7,8]])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 can use indexing:
>>> x[x[:,3] != 1] array([[ 1, 2, 3, 0], [11, 2, 3, 24], [ 5, 6, 7, 8]])
这篇关于指数误差,从阵列中删除行,如果列有一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文