I would like to filter a datframe that has association rules results. I want antecedents that contain an element like H or L in my case. The antecedents are frozenset types. I tried Hrules but it is not working.
Hrules=fdem_rules['H' in fdem_rules['antecedents']]
Hrules=fdem_rules[frozenset({'H'}) in fdem_rules['antecedents']]
did not work
In the following example, I need only rows 46 and 89 as they have 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)
set/frozenset methods
You can use
applywith set/frozenset's method. Here to check is at least H or L is present, one can use the negation of{'H', 'L'}.isdisjoint:A much faster variant of the above is to use a list comprehension:
explode+isin+aggregate
Another option is to
explodethe frozenset, useisin, and aggregate the result withgroupby+any:output:
slicing matching rows
output: