I have a dataset which contains orders, and items in those orders. What I want to find is which item pairs exist together in which group. For example, I want to find orders which has 6395477 and 6391546 together, in this case order 20220627-0015 and 20220627-0014 have them.
ORDER_ID ITEM_ID
20220627-0015 6395477
20220627-0015 6391546
20220627-0015 6385826
20220627-0015 6385822
20220627-0015 6385810
20220627-0015 6385797
20220627-0015 6379808
20220627-0014 6395477
20220627-0014 6391546
20220627-0014 6386347
20220627-0014 6385814
20220627-0014 6385812
20220627-0014 6385810
20220627-0014 6384898
20220627-0013 6395592
20220627-0013 6395583
20220627-0013 6394927
20220627-0013 6386403
20220627-0013 6384293
20220627-0013 6380162
20220627-0013 6379830
20220627-0013 6379828
I created the item pairs from the items list using the code:
x= [tuple(x) for i,x in items.iteritems() for x in combinations(x,2) ]
and then I found the orders that contains 1900th elements of the x together with the code:
orders=df.loc[df['Item_Id'].isin(x[1900])].groupby('Order_Id', as_index=False).nunique()
order_list=orders.loc[orders['Item_Id'] == len(x[1901]), 'Order_Id'].to_list()
and I get the result as I wanted to:
[91158728,91158755,91158756,'20220627-0015']
I created a function to easily loop over the item pairs and tested it:
def ordfunc(df1,ls1,i):
orders = df1.loc[df1['Item_Id'].isin(ls1[i])].groupby('Order_Id', as_index=False).nunique()
order_list=orders.loc[orders['Item_Id'] == len(ls1[i]), 'Order_Id'].to_list()
return order_list
I tried to get the result with the loop below but kernel couldn't handle it
final_list = [list(ordfunc(df,x,i)) for i in range(len(x)) ]
How can I create the loop that iterates over roughly 1.7 m item pairs and gives the list of list that contains those pairs together?