python - 如何附加索引

标签 python pandas if-statement counter

我有以下代码:

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 并那么所有这些行都将被删除。

最佳答案

您正在尝试并行执行两件事,删除索引并计算 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)

关于python - 如何附加索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55913686/

相关文章:

javascript - 关于 Javascript For 循环

Python数字基类或如何确定一个值是一个数字

python - MySQLdb模块真实文件路径在哪里?

python - 当 json_normalize 无法迭代列以展平时如何修复它?

python - Pandas 中的“逆”cumprod

javascript - 为什么我的 JavaScript 代码没有按预期运行?

python - 机器学习: Getting error in Confusion Matrix

Python XPath 语法错误 : invalid predicate

python - 在 Python 中复制 %transpose SAS 宏

javascript - For 循环内的 If/Else 语句所需的语法