gpt4 book ai didi

python - 如何附加索引

转载 作者:行者123 更新时间:2023-12-01 07:57:10 26 4
gpt4 key购买 nike

我有以下代码:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com