如何在while循环中正确设置增量?

时间:2018-09-25 13:37:48

标签: python-3.x dataframe if-statement while-loop

我有一个数据框(DF) 我需要遍历每一行,并检查该行是否满足某些条件 如果它们然后标记了该行(例如,我添加了另一列标记为“ flag”的列并将其等于1),则在同一循环中检查是否存在其他具有类似条件的行,如果存在,则也对其进行标记。在下一个循环中,查看相同的DF,但排除标记的行。 DF的大小将从NxM变为(N-n)x M,其中n是标记的行数。 循环将继续进行,直到len(DF)<= 1(意味着直到所有行都标记为1)为止。 for循环不起作用,因为随着循环的进行,DF的大小会缩小,因此我只能使用带有增量的while循环。但是,如何设置增量(它应该是动态的)?

我真的不确定如何解决这个问题。

这是失败的尝试。

a=len(DF.loc[DF['flag'] != 1]) #should be  (NxM) initially
i = 0
# at every loop we redefine size of DF in variable a
while a >= 1:
        print(i)
        # select first row          
        row = DF.loc[DF['flag'] != 1].iloc[[i]]
        # flag row if conditions are met
        DF['flag'].values[i] = np.where(if conditions met, 1, '')

        #there is another piece of code that looks for rows with similar 
        #conditions but won't add it here

        # the following variable a redefines length of DF 
        a=len(allHoldingsLookUp.loc[allHoldingsLookUp['flag'] != 1])
        i+=1

我在这里有问题。增量我不工作。说“ i”达到100,而DF的长度缩小到70,则代码失败。需要以不同的方式设置增长,但不确定如何。 任何意见或建议都将受到欢迎。

2 个答案:

答案 0 :(得分:1)

如果此更改有效,请尝试。.

a=len(DF.loc[DF['flag'] != 1]) #should be  (NxM) initially
# at every loop we redefine size of DF in variable a
while a >= 1:
        i = 1
        # select first row          
        row = DF.loc[DF['flag'] != 1].iloc[[i]]
        # flag row if conditions are met
        DF['flag'].values[i] = np.where(if conditions met, 1, '')

        #there is another piece of code that looks for rows with similar 
        #conditions but won't add it here

        # the following variable a redefines length of DF
        try:
            a=len(DF.loc[DF['flag'] != 1])
        except:
            break

答案 1 :(得分:1)

可以请您尝试一下。希望它能工作。

def recur(DF):
  row = DF.loc[DF['flag'] != 1].iloc[[1]]            
  DF['flag'].values[1] = np.where(if conditions met, 1, '')
  #there is another piece of code that looks for rows with similar 
  #conditions but won't add it here 
  # the following variable a redefines length of DF 
  a=len(DF.loc[DF['flag'] != 1])
  if a >= 1:
    recur(DF.loc[DF['flag'] != 1])
  return none