【问题标题】:How to append indices如何附加索引
【发布时间】:2019-04-30 03:45:54
【问题描述】:

我有以下代码:

indices_to_remove= []
for i in range(0,len(df)):
        if (df.speed.values[i] <= 15 ):
            counter += 1
            if counter > 600:
               indices_to_remove.append(i)        

        else:
            counter= 0

df= df.drop (indices_to_remove, axis=0)

此代码的主要目标是遍历我的数据集中的所有行,以防有超过 600 个连续行的速度值小于 15。代码会将行索引添加到 indices_to_remove 和那么所有这些行都将被删除。

【问题讨论】:

  • 你能分享一下你的数据框是什么样子的吗!
  • 我编辑了帖子以回答您的问题。我希望你能帮忙。所以基本上我希望删除从零到 1000 的行。
  • 如果该行有value&lt;15,你想删除它吗?你能澄清一下你counter&gt;600的情况吗~
  • 是的,没错。删除前需要满足两个条件。首先,值
  • 另外,请记住,我有不止一组 600 多个连续行的值小于 15。@DeveshKumarSingh

标签: python pandas if-statement counter


【解决方案1】:

您正在尝试并行执行两件事,获取要删除的索引并计算 600 个小于 15 的连续值。我会将这两个想法分为两个步骤。

  1. 查找值小于 15 的所有索引
  2. 之后,计算连续的索引
  3. 如果这些索引超过 600 个,请执行删除
indices_to_remove= []

#Get all indexes to remove from the dataframe
for i in range(0,len(df)):
    if (df.speed.values[i] <= 15 ):
        indices_to_remove.append(i)

#Have a counter which keeps track of 600 consecutive indexes less than 15
counter = 0
max_counter = -1
for idx in range(len(indices_to_remove)-1):

    #If the indexes were consecutive, keep a counter
    if ((indices_to_remove[idx+1] - indices_to_remove[idx]) == 1):
        counter += 1

    #Else if non consecutive indexes are found, track the last maximum counter and reset the original counter
    else:
        if counter > max_counter:
            max_counter = counter
        counter = 0

if max_counter > 600:
    df = df.drop(indices_to_remove, axis=0)

【讨论】:

    【解决方案2】:

    不是一个优雅的解决方案,而是建立在你现有的基础上:

    indices_to_remove= []
    indices_counter = []
    for i in range(0,len(df)):
    
            if (df.speed.values[i] <= 15 ):
                counter += 1
    
                indices_counter.append(i)
    
                if counter > 600:
                    commit = True
    
            elif commit:
                indices_to_remove.extend(indices_counter)
                indices_counter = []
                commit = False
                counter= 0
    
            else:
                indices_counter = []
                commit = False
                counter= 0
    
    df= df.drop(indices_to_remove, axis=0)
    

    【讨论】:

      【解决方案3】:

      构建连续速度小于或等于 15 的索引的字典。

      indices_dict = {}
      k = 0
      for i in range(0, len(df)):
          if (df.speed.values[i] <= 15 ):
              try:
                  indices_dict[k].append(i)
              except KeyError:
                  indices_dict[k] = [i]
          else:
              k += 1
      
      lol_to_remove = [ v for k,v in indices_dict.items() if len(v)>= 600 ] # This is a list of lists (lol)
      indices_to_remove = [i for v in lol_to_remove for i in v ] # flatten the list
      
      df = df.drop(indices_to_remove, axis=0)
      

      【讨论】:

        猜你喜欢
        • 2015-11-29
        • 2013-04-21
        • 2021-10-04
        • 1970-01-01
        • 2015-08-03
        • 2016-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多