我有以下代码:
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 的连续值。我会将这两个想法分为两个步骤。
- 查找值小于 15 的所有索引
- 之后,统计连续的索引
- 如果这些索引超过 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/