使用Apriori后无法使用Pandas编辑数据框

时间:2019-07-18 09:43:30

标签: python-3.x pandas apriori frozenset

我正在制作要由其他用户运行的购物篮分析代码。一切工作正常,但是输出不是很友好,我无法在数据框中进行任何更改。

我尝试过一些简单的任务,例如删除列,但熊猫似乎无法使用此数据集。

import pandas as pd
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules
import os


path = os.path.join(os.path.expanduser('~'), 'Desktop', 'Base.xlsx')
path_exp = os.path.join(os.path.expanduser('~'), 'Desktop', 'Cesta.xlsx')

# reading file
df1 = pd.read_excel(path)
# renaming columns
df1.columns = ['Codigo_Pedido', 'Categoria', 'Volume']

# Creating basket (Turning rows to columns)
basket = (df1
          .groupby(['Codigo_Pedido', 'Categoria'])['Volume']
          .sum().unstack().reset_index().fillna(0)
          .set_index('Codigo_Pedido'))

def encode_units(x):
    if x <= 0:
        return 0
    if x >= 1:
        return 1
basket_sets = basket.applymap(encode_units)

frequent_itemsets = apriori(basket_sets, min_support=0.3, use_colnames=True)
rules = association_rules(frequent_itemsets, metric="lift", min_threshold=1)
rules2 = rules[(rules['confidence'] >= 0.3)]

# Exporting to excel
rules2 = rules2.to_dict()
rules3 = pd.DataFrame(data=rules2)
rules3.to_excel(path_exp)

# Editing to be more friendly
dfnovo = pd.read_excel(path_exp)
dfnovo['copia'] = dfnovo['antecedents'] 
dfnovo.drop(columns=['copia'], axis = 1)

我没有收到任何错误消息,但是数据集根本没有任何变化。

0 个答案:

没有答案