在 pandas 中一起购买的最受欢迎的商品组合

Most popular item combinations bought together in pandas

我有一个 df:

product_id          order_id            category            qty_sold        sale_value
table_1             1                   Tables              1               999
chair_1             1                   Chairs              4               1000
table_1             2                   Tables              1               999
chair_1             2                   Chairs              2               500
table_2             3                   Tables              1               999
chair_1             3                   Chairs              4               1000
table_2             4                   Tables              1               999
chair_2             4                   Chairs              8               2500
table_1             5                   Tables              1               999
chair_1             5                   Chairs              2               500

是椅子和table的顺序数据,每一行有一个order_id和一个product_id。我正在尝试回答 哪些是最畅销的 table 和最畅销的椅子 的最流行组合。 所以基本上,我会把 table 中的前 50 product_id 个分配给每个 table product_id 我会分配前 n product_ids 个用那个买的椅子table。 用那个买的意思是它共享相同的order_id

我尝试创建的所需输出如下所示:

df_tables - qty_sold

最受欢迎的 table
product_id              qty_sold            chair_product_id            chairs_sold         times_sold         
table_1                 3                   chair_1                     6                   2                        
                                            chair_3                     2                   1
                            



table_2                 2                   chair_1                     4                   1
                                            chair_2                     8                   1

通过匹配 order_id 并计算每个组合一起出现的次数,简单地将 tableschairs 组合在一起。

我试过了:

# Getting the list of most popular table product_ids

top_tables = df[df.category == 'Tables'] \
                .groupby('product_id').sum().reset_index() \
                .sort_values('qty_sold', ascending = False) \
                .product_id.to_list()       

most_popular_table = df[df.product_id == top_tables[0]].order_id.unique().tolist()

# getting top chairs bought with most popular table
df[(df.order_id.isin(most_popular_table)) & (df.category == 'Chairs')] \
               .groupby('product_id').agg({'qty_ordered' : sum}) \
               .sort_values('qty_ordered', ascending = False).reset_index()

返回(来自真实数据的样本)

product_id      qty_ordered
4384            661
9974            247
8310            213
4032            166
9891            138

但是我想不出一种方法来创建我上面提到的每个前 50 tables 的所需输出,然后是我检查 top chairs & tables 的第二个输出.我想知道是否有一种巧妙的方法来获得我想要的结构。

为桌子和椅子创建一个 DataFrame(添加列前缀以区分它们),然后使用 order_id 作为索引将它们连接起来。按 product_id 对整体进行分组并汇总以获得售出的商品数量:

df_chairs = df[df['category']=='Chairs'].set_index('order_id').drop(['category', 'sale_value'], axis=1).add_prefix('chair_')
df_tables = df[df['category']=='Tables'].set_index('order_id').drop(['category', 'sale_value'], axis=1).add_prefix('table_')

df = df_tables.join(df_chairs)

df_out = df.groupby(['table_product_id', 'chair_product_id']).agg(sum)
df_out['times_sold'] = df.groupby(['table_product_id', 'chair_product_id']).size()
print(df_out)

输出:

                                   table_qty_sold  chair_qty_sold  times_sold
table_product_id chair_product_id                                            
table_1          chair_1                        3               8           3
table_2          chair_1                        1               4           1
                 chair_2                        1               8           1