索引器错误:numpy数组中的元组索引超出范围

2024-10-01 04:59:11 发布

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

我正在用numpy和pandas做一些矩阵值替换。我的主要目标是将矩阵ds2中的索引值匹配到ds1中,并用相应的值替换它。这个代码片段如预期的那样工作。在

def missingValueProcessing(self ):
    ds1 = [[ 4, 13,  6,  9],
          [ 7, 12,  5,  7],
          [ 7,  0,  4, 22],
          [ 9,  8, 12,  0]]

    ds2 = [[ 4,  1],
           [ 5,  3],
           [ 6,  1],
           [ 7,  2],
           [ 8,  2],
           [ 9,  3],
           [12,  1],
           [13,  2],
           [22,  3]]

    ds1= pd.DataFrame(ds1)
    ds2= pd.DataFrame(ds2)

    #Processing ds1 by replacing
    ds2.groupby(0, as_index=False).mean()
    C = np.where(ds1.values.ravel()[:, None] == ds2.values[:, 0])
    print C[1]

当我对实际相似的大矩阵做同样的处理时

^{pr2}$

它给予

  File "missing_value.py", line 15, in missingValueProcessing
    print C[1]
IndexError: tuple index out of range

任何建议都是值得赞赏的


Tags: 代码selfnumpy目标dataframepandasindexdef