Ere*_*she 3 python apriori market-basket-analysis frozenset mlxtend
有以下声明:
rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1.2)
Run Code Online (Sandbox Code Playgroud)
我得到一个格式的规则数据框:
frozenset({'Co_Apples'})
Run Code Online (Sandbox Code Playgroud)
但我需要将 a 提取Co_Apples为字符串。
我怎样才能做到这一点?
小智 6
您可以使用以下代码从 freezeset 类型列获取字符串,然后将该字符串转换为 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")
Run Code Online (Sandbox Code Playgroud)
小智 5
rules["antecedents"] = rules["antecedents"].apply(lambda x: ', '.join(list(x))).astype("unicode")
Run Code Online (Sandbox Code Playgroud)
It is work for me. Thanks Frank Herfert save my day!