检查NDArray列中的Nan值并将其删除

2024-06-28 19:38:09 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的代码是为了检查Python ND数组列中的NaN值而编写的。如果temparr1或temparr2中有一个NaN,我们将从它们中删除相应的行。问题是,它似乎不起作用。你能帮帮我吗?在

        temparr1=arr[index[indexkey]][:]// We get a column from arr, an nd-array of size 0 to 9470
        temparr2=arr[index[secondIndexKey]][:]// Same as above, but with the next column
        rwc=range(0,len(arr)) We get a bit vector of a sort to check.
        for i in range(0,len(arr)):
            if(isnan(temparr1[i]) or isnan(temparr2[i]) ):
                rwc = rwc[:i-1]+rwc[i+1:] // Remove the value from the bit Vector for a NaN value in the arrays.
                print i
        temparr1 = []
        temparr2 = []
        for i in rwc:
            temparr1.append(arr[index[indexkey]][i])
            temparr2.append(arr[index[secondIndexKey]][i])// Extract the data for the corresponding values in RWC and get them into the temparrs.

有人能告诉我为什么它不起作用,为什么我仍然得到南斯??在

数组如下:[99242122,nan,42,nan,414,……………]


Tags: theinfromforgetindexcolumn数组
1条回答
网友
1楼 · 发布于 2024-06-28 19:38:09

rwc=range(0,len(arr))之后是len(rwc)=len(arr),所以在rwc = rwc[:i-1]+rwc[i+1:]行中,i是{}和{}的相同索引。在

但是,在rwc = rwc[:i-1]+rwc[i+1:]之后,您将得到一个长度更小的列表(len(rwc) = len(arr) -2),因此在下一次迭代中,您将开始从列表中删除错误的元素。在

我还怀疑您打算做rwc = rwc[:i]+rwc[i+1:],这是另一个bug

据我所知你试图做这样的事:

X=arr[index[indexkey]]
Y=arr[index[secondIndexKey]]

temparr1 = []
temparr2 = []
for i in range(len(X)):  #I assume len(X)=len(Y)
    if not (isnan(X[i]) or isnan(Y[i])):
        temparr1.append(X[i])
        temparr2.append(Y[i])

相关问题 更多 >