我想过滤掉至少n行值相等的数据点。
具体来说,检测至少 n 行的相等值,并过滤掉所有连续的相等数据。
这里是 n = 6 且扁线有 8 行长的示例:
import pandas as pd
data={'col1':[1, 3, 3, 3, 3, 3, 3, 3, 3, 4, 1, 1, 1, 1, 1]}
df=pd.DataFrame(data,columns=['col1'])
print df
col1
0 1
1 3
2 3
3 3
4 3
5 3
6 3
7 3
8 3
9 4
10 1
11 1
12 1
13 1
14 1
我想获得以下 pd.Series“flatline_filter”:
s flatline_filter
0 1 True
1 3 False
2 3 False
3 3 False
4 3 False
5 3 False
6 3 False
7 3 False
8 3 False
9 4 True
10 1 True
11 1 True
12 1 True
13 1 True
14 1 True
在某个点过滤掉那些扁平线:
df = df[flatline_filter]
groupby.transform
:
n = 5
df['flatline_filter'] = (~df.groupby(df['col1'].diff().ne(0).cumsum())
.transform('size').gt(n)
)
输出:
col1 flatline_filter
0 1 True
1 3 False
2 3 False
3 3 False
4 3 False
5 3 False
6 3 False
7 3 False
8 3 False
9 4 True
10 1 True
11 1 True
12 1 True
13 1 True
14 1 True