iterrows() 时从 DataFrame 读取错误值

Wrong value read from DataFrame while iterrows()

我构建了一个代表层次结构的数据框。现在我正在尝试迭代地遍历这个数据框(迭代虽然不好但我认为只有这适用于我的情况)根据我的程序计算更改数据框的值并删除行。

对于前两次迭代,程序正确读取了数据框行。然而,在第三次迭代中(总是),它正在读取我认为的旧值。

以防万一,我没说清楚,这里是ipython笔记本:Ipython Notebook

初始数据帧:

        c1   c2  c3    c4
   19   21   20  14  0.718004
   18   20   21  14  0.749271
   17   19   18  17  0.724873
   16   18   19  17  0.647143
   15   17   16  11  0.348749
   14   16   17  11  0.847751
   13   15   14   9  0.597245
   12   14   15   9  0.596115
   11   13   12   8  0.549009
   10   12   13   8  0.810719
    9    11   10   7  0.328420
    8    10   11   7  0.859816
    7     9    8   6  0.449287
    6     8    9   6  0.724799
    5     7    6   4  0.320076
    4     6    7   4  0.306391
    3     5    4   2  0.809620
    2     4    5   2  0.450804
    1     3    2   1  0.771699
    0     2    3   1  0.118202

有问题的代码:

computed_dataframe.sort_values(['c1'], ascending=0, inplace = True)
for index, row in computed_dataframe.iterrows():
    print computed_dataframe
    print row['c3']
    if row['c3'] == 1:
        break
    select_final(row['c3'])
    print computed_dataframe

函数定义

def select_final(check_label): 
.....
.....
    parent_frame = computed_dataframe[computed_dataframe['c1'] == check_label]

    parent_score = get_parent_row_frame.iloc[0]['c4']
    if avg > parent_score:
        for i in child_index:
           computed_dataframe.loc[i,'c2'] = parent_row_frame.iloc[0]['c3']
     computed_dataframe = computed_dataframe[computed_dataframe.c1 != parent_frame.iloc[0]['c1']]
    elif avg <= parent_score:
     computed_dataframe = computed_dataframe[computed_dataframe.c3 != check_label]

return

迭代 1:行 ['c3'] 指向 14

第一次迭代的结果帧:

        c1   c2  c3    c4
   19   21   20  9  0.718004
   18   20   21  9  0.749271
   17   19   18  17  0.724873
   16   18   19  17  0.647143
   15   17   16  11  0.348749
   14   16   17  11  0.847751
   13   15   14   9  0.597245
   # Deleted with c1 = 14 
   11   13   12   8  0.549009
   10   12   13   8  0.810719
   ......
   ......

迭代 2:读取索引 18.row['c3'] 指向 9

第二次迭代的结果帧:

        c1   c2  c3    c4
   19   21   20  6  0.718004
   18   20   21  6  0.749271
   17   19   18  17  0.724873
   16   18   19  17  0.647143
   15   17   16  11  0.348749
   .....
   # Deleted row with c1 = 9 
   .........

迭代 3:读取索引 17.row['c3'] 指向 17

第 3 次迭代的结果帧:

        c1   c2  c3    c4
   19   21   20  6  0.718004
   18   20   21  6  0.749271
   17   19   18  11  0.724873
   16   18   19  11  0.647143
   #Deleted row with c1 17
   14   16   17  11  0.8477
   .....
   ..... 
   .........

迭代 4:读取索引 16。行 ['c3'] 指向 11。

但是,程序仍然以某种方式将行['c3']读取为17然后我在执行函数时运行进入错误,因为它在 c1 中找不到与 17 对应的任何匹配项,因此,我的计算导致除以零错误。我无法理解它仍然从哪里读取 c3 为 17。打印的数据框显示该索引处的更新值为 11。

有人能帮我解决这个错误以及它是从哪里弹出的吗?

这是我的错误。我正在更新我正在迭代的同一个数据框。