我想过滤一个具有关联规则结果的数据帧。在我的例子中,我想要包含像H或L这样的元素的前因。其前缘为冻融型。我试过了,但没有用。
Hrules=fdem_rules['H' in fdem_rules['antecedents']]
Hrules=fdem_rules[frozenset({'H'}) in fdem_rules['antecedents']] 不管用
在下面的示例中,我只需要第46行和第89行,因为它们有H。
df = pd.DataFrame({'antecedents': [frozenset({'N', 'M', '60'}), frozenset({'H', 'AorE'}), frozenset({'0-35', 'H', 'AorE', '60'}), frozenset({'AorE', 'M', '60', '0'}), frozenset({'0-35', 'F'})]}) antecedents
75 (N, M, 60)
46 (H, AorE)
89 (0-35, H, AorE, 60)
103 (AorE, M, 60, 0)
38 (0-35, F)发布于 2022-01-05 08:47:01
set/frozenset方法
您可以在set/frozenset的方法中使用apply。这里检查是否至少存在H或L,可以使用{'H', 'L'}.isdisjoint的否定
match = {'H', 'L'}
df['H or L'] = ~df['antecedents'].apply(match.isdisjoint)上面的一个更快的变体是使用列表理解:
match = {'H', 'L'}
df['H or L'] = [not match.isdisjoint(x) for x in df['antecedents']]explode+isin+aggregate
另一个选项是使用explode,使用isin,并使用groupby+any聚合结果
match = {'H', 'L'}
df['H or L'] = df['antecedents'].explode().isin(match).groupby(level=0).any()产出:
>>> df[['antecedents', 'H or L']]
antecedents H or L
75 (N, M, 60) False
46 (H, AorE) True
89 (0-35, H, AorE, 60) True
103 (AorE, M, 60, 0) False
38 (0-35, F) False切片匹配行
match = {'H', 'L'}
idx = [not match.isdisjoint(x) for x in df['antecedents']]
df[idx]产出:
antecedents consequents other_cols
46 (H, AorE) (N) ...
89 (0-35, H, AorE, 60) (0) ...https://stackoverflow.com/questions/70589625
复制相似问题