我有一个由布尔值组成的数据帧。我想在数据帧中匹配某些多列模式。模式如下所示:
bar foo
0 False True
1 True False
预期输出如下所示:
foo bar pattern
0 True False False
1 True False False
2 True False True
3 False True False
4 False True False
5 False True False
6 False False False
7 False False False
8 False False False
9 False True False
10 False True False
11 False True False
12 False True False
13 False True False
14 False True False
15 False True False
16 True False False
17 True False False
18 True False True
19 False True False
20 False True False
21 False True False
22 True False True
23 False True False
24 False True False
25 False True False
我提出了我自己的实现,但我想应该有一个更好的。你知道吗
def matcher(df, pattern):
def aggregator(pattern):
"""Returns a dict of columnswith their aggregator function
which is the partially applied inner in this case"""
def inner(col, window):
return (window == pattern[col]).all()
return {col: partial(inner, col) for col in pattern.columns}
aggregated = (df
# Feed the chunks to aggregator in `len(pattern)` sized windows
.rolling(len(pattern))
.aggregate(aggregator(pattern))
# I'd like it to return True at the beginning of the match
.shift(-len(pattern) + 1)
# rows consisting of nan return true to `.all()`
.fillna(False))
ret = [row.all() for _, row in aggregated.iterrows()]
return pd.Series(ret)
我最担心的是处理nan值,以及缺少通配符支持(为了支持不一定是长方体模式)。你知道吗
有什么建议吗?你知道吗
如果
pd.concat()
对您来说不是太贵,那么下面的代码将非常高效地工作,因为没有循环和嵌套函数。你知道吗假设df1是您的模式df,df2是您的值df,您可以使用apply检查模式。对于每一行,我们得到当前行和下一行,然后将2*2数组与df1元素else进行比较,检查所有元素是否相同。你知道吗
相关问题 更多 >
编程相关推荐