Extract string from rules frozensets

Viewed 7843

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?

2 Answers
rules["antecedents"] = rules["antecedents"].apply(lambda x: ', '.join(list(x))).astype("unicode")

It is work for me. Thanks Frank Herfert save my day!

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")
Related