2013-01-25 36 views
0

下面的代碼是爲了檢查Python ND-Array列中的NaN值而編寫的。如果temparr1或temparr2中有NaN,我們從它們兩個中刪除相應的行。問題是,它似乎沒有工作。你能幫我解決嗎?檢查ND陣列中的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. 

有人可以告訴我爲什麼它不工作,爲什麼我仍然得到NaNs?

的Array看起來像:[99242122,楠,42,楠,414,................]

回答

1

rwc=range(0,len(arr))之後,你有len(rwc)=len(arr),所以在行rwc = rwc[:i-1]+rwc[i+1:]您預計irwcarr相同的索引。

你做rwc = rwc[:i-1]+rwc[i+1:]後,但是你得到更小的長度(len(rwc) = len(arr) -2)的列表,以便下一次迭代過程中,你開始刪除從列表中錯誤的元素。

而且我懷疑你打算做rwc = rwc[:i]+rwc[i+1:],這是另一種錯誤

據我瞭解你試圖做這樣的事情:

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]) 
+0

感謝的人...ü可能會如何改正這個? –