Python提供了如何在两列之间找到3个组合的计数

Python提供了如何在两列之间找到3个组合的计数,python,pandas,Python,Pandas,我试图找出熊猫的收据中产品组合的数量。通过一些帮助,我在前面的问题中找到了两种产品的组合,但我仍然怀疑如何实现这一结果,以及如何升级以找到更多的组合 我在数据框中有两列,一列是收据,另一列是购买的产品: 收据\u id产品\u名称 一个苹果 1面包 一杯可乐 2苹果 2可乐 2面包 3苹果 3可乐 4苹果 4可乐 4面包 我想在收据中找到3种产品组合的数量。所以,对于这个例子,结果应该是这样的: product1 product2 product3计数 苹果面包可乐3 这意味着:第一个产品组

我试图找出熊猫的收据中产品组合的数量。通过一些帮助,我在前面的问题中找到了两种产品的组合,但我仍然怀疑如何实现这一结果,以及如何升级以找到更多的组合

我在数据框中有两列,一列是收据,另一列是购买的产品:

收据\u id产品\u名称
一个苹果
1面包
一杯可乐
2苹果
2可乐
2面包
3苹果
3可乐
4苹果
4可乐
4面包
我想在收据中找到3种产品组合的数量。所以,对于这个例子,结果应该是这样的:

product1 product2 product3计数
苹果面包可乐3
这意味着:第一个产品组合出现在3张收据中。“苹果”和“可乐”的数量没有出现,因为它只是两种产品的组合


我知道我需要使用groupby函数,但现在确定了如何在同一列中组织数据。任何帮助都是值得的!提前谢谢

虽然它很长,但仍然有效

首先,我创建了一个新的数据框架,您可以在其中获得产品的所有独特组合。我在等式中加了“大米”,因为当你有4种不同的产品时,你有3种产品的4种组合

import pandas as pd
from itertools import combinations

 df = pd.DataFrame({'receipt_id': [1,1,1,2,2,2,3,3,4,4,4],
                'product_name': ['apple', 'bread', 'rice', 'apple', 'rice', 'bread', 'apple', 'cola', 'apple', 'cola', 'bread']})

df
      receipt_id product_name
0            1        apple
1            1        bread
2            1         rice
3            2        apple
4            2         rice
5            2        bread
6            3        apple
7            3         cola
8            4        apple
9            4         cola
10           4        bread
然后,我获得了独特的产品及其所有组合

unique_products = df.product_name.unique().tolist() #get the unique products
combo = list(combinations(unique_products, 3)) #get a list with all combinations

new_df = pd.DataFrame()

new_df['Product_one'] = [e[0] for e in combo] #only the first element in each tuple
new_df['Product_two'] = [e[1] for e in combo] #the second element
new_df['Product_three'] = [e[2] for e in combo] #the third 
现在我们有了一个数据框,所有组合都是行:

    Product_one Product_two Product_three
0       apple       bread          rice
1       apple       bread          cola
2       apple        rice          cola
3       bread        rice          cola
然后我创建了一个函数,它接受三个列表作为输入,并返回一个只包含三个列表中存在的值的列表。我在原始数据帧上做了一个for循环,并计算了与每个产品相关的id

def get_unique(l1, l2, l3):
#take the 3 lists and return only those elements present in the 3 lists 
    return [e for e in l2 if e in l1 and e in l3]


unique_ids = []
for i in range(len(new_df)):

    list_one = df.receipt_id[(df.product_name == new_df.iloc[i,0])].unique()
    list_two = df.receipt_id[(df.product_name == new_df.iloc[i,1])].unique()
    list_three = df.receipt_id[(df.product_name == new_df.iloc[i,2])].unique()

    unique_ids.append(get_unique(list_one, list_two, list_three))
现在这个列表包含了其他列表,其中包含了满足上述条件的id。然后,我添加了一个新列,其中包含每个元素的长度

new_df['count'] = [len(e) for e in unique_ids]
print(new_df)

    Product_one Product_two Product_three  count
0       apple       bread          rice      2
1       apple       bread          cola      1
2       apple        rice          cola      0
3       bread        rice          cola      0

一个类似的问题,对于成对的而不是三胞胎:但是如何用3个项目来做呢?我一直在尝试你的答案,但我不能这样做。我有一个很长的和低效的答案,但它的工作。你还想看吗?当然,欢迎任何帮助!这回答了你的问题吗?这应该行得通。告诉我它是否适用于您的数据集!这真是太好了!我能够理解正在发生的一切,非常感谢!我将尝试将此应用于我现在使用的数据帧,再次感谢:D