pythonapriorimarket-basket-analysisfrozensetmlxtend

Extract string from rules frozensets


With the following statement:

rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1.2) 

I get a data frame of rules in the format:

frozenset({'Co_Apples'})

But I need to extract a Co_Apples as a string.

How can I do that?


Solution

  • You can use the following code to get a string from frozenset type columns and then cast the string to unicode.

    rules["antecedents"] = rules["antecedents"].apply(lambda x: list(x)[0]).astype("unicode")
    rules["consequents"] = rules["consequents"].apply(lambda x: list(x)[0]).astype("unicode")