6

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?

Erez Ben-Moshe
  • 149
  • 2
  • 3
  • 10

2 Answers2

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

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

Johnny Hsiao
  • 81
  • 1
  • 2
5

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")
  • 5
    In case you have item combinations (a frozenset with more than one value), using `list(x)[0]` will only show the first value. You can use `', '.join( list(x) )` instead to separate the items with a comma. `rules["antecedents"] = rules["antecedents"].apply(lambda x: ', '.join(list(x))).astype("unicode")` – Frank Herfert Jul 18 '19 at 15:25