代码之家  ›  专栏  ›  技术社区  ›  SBad

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

  •  1
  • SBad  · 技术社区  · 6 年前

    我有一个数据帧(DF) 如果它们随后标记该行(假设我添加另一个标记为“flag”的列并将其相等为1)-在同一循环中检查是否有其他行具有类似的条件,如果有,则也标记它们。在下一个循环中,查看相同的DF,但排除标记的行。DF的大小将从NxM变为(N-N)x M,其中N是标记的行数。 循环将继续,直到len(DF)<=1(表示直到所有行都标记为1)。for循环不起作用,因为随着循环的进行,DF的大小会缩小,所以我只能使用while循环和increment。但是,如何设置增量(应该是动态的)?

    这是一次失败的尝试。

    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
    

    欢迎提出任何意见或建议。

    2 回复  |  直到 6 年前
        1
  •  1
  •   Arihant    6 年前

    请试试这个改变是否有效。。

    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
    
        2
  •  1
  •   Arihant    6 年前

    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