【问题标题】:Check for Nan values in the column of an ND-Array and Remove them检查 ND-Array 列中的 Nan 值并删除它们
【发布时间】:2013-01-09 01:40:25
【问题描述】:

编写下面的代码是为了检查 Python ND-Array 列中的 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.

谁能告诉我为什么它不起作用,为什么我仍然得到 NaN??

一个数组看起来像:[99,242,122,nan,42,nan,414,........]

【问题讨论】:

    标签: python loops slice bitvector multidimensional-array


    【解决方案1】:

    在rwc=range(0,len(arr)) 之后是len(rwc)=len(arr),因此在rwc = rwc[:i-1]+rwc[i+1:] 行中,您希望i 是rwc 和arr 的相同索引。

    但是,在您执行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])
    

    【讨论】:

      猜你喜欢
      • 2020-08-04
      • 2014-03-07
      • 2018-04-19
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-18
      相关资源
      最近更新 更多