Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 滚动窗口-标记值_Python_Pandas - Fatal编程技术网

Python 滚动窗口-标记值

Python 滚动窗口-标记值,python,pandas,Python,Pandas,在一个数据帧中,我想过滤一些列在10.0个单位内稳定的行 def abs_delta_fn(window): x = window[0] for y in window[1:]: if abs(x-y) > 10.0: return False return True df['filter']= df['column'].rolling(5, min_periods=5).apply(a

在一个数据帧中,我想过滤一些列在10.0个单位内稳定的行

def abs_delta_fn(window):
   x = window[0]            
   for y in window[1:]:
      if abs(x-y) > 10.0:
         return False            
      return True

df['filter']= df['column'].rolling(5, min_periods=5).apply(abs_delta)
如果a有这样的df

1   0
2   20
3   40
4   40
5   40
6   40
7   40
8   90
9   120
10  120
应用我得到的滚动窗口:

1   0     nan
2   20    nan
3   40    nan
4   40    nan
5   40    False
6   40    False
7   40    True
8   90    False
9   120   False
10  120   False
我怎样才能聪明地得到这个

1  0     nan (or False)
2  20    nan (or False)
3  40    True
4  40    True
5  40    True
6  40    True
7  40    True
8  90    False
9  120   False
10 120   False

IIUC,您已经知道了
滚动
,只需添加
应用
,然后,这里的键是
.iloc[:-1]
,因为滚动是从当前行向上看(向后),但您需要向前看

s=df.x.iloc[::-1].rolling(5,min_periods=5).apply(lambda x : (abs((x-x[0]))<10).all())
df.loc[df.index.difference(sum([list(range(x, x+5))for x in s[s==1].index.values],[]))]

Out[1119]: 
      x
1     0
2    20
8    90
9   120
10  120

s=df.x.iloc[::-1]。滚动(5,最小周期=5)。应用(λx:(abs((x-x[0]))你能进一步解释一下你的预期输出吗?有5个连续的相等或接近相等的值。这些值是40,40,40,40,40。每次出现这种情况时,我都想过滤数据帧中的任何值。我可以使用for循环并执行此操作,但我宁愿不…@guilhermecgs我不确定我的答案是否能解决你的问题,但看起来像实现你需要的目标给我一分钟时间来测试它我想你明白了…这不是一个非常有效的解决方案,但我不认为有一个。。。