熊猫滚动窗口-标记值

吉尔赫梅格斯

在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(abs_delta)

所以,如果一个像这样的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
BEN_YO

IIUC,您已经知道rolling,只需apply在其后添加,这里的关键是.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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章