by taking similar case here is what i did
df=pd.DataFrame({'product1':[1277,3921,1277,3921],'product2':[1854,2561,9132,5143]})
product1 product2
0 1277 1854
1 3921 2561
2 1277 9132
3 3921 5143
df2=pd.DataFrame({'customer':['a1','a1','a1','a2','a2','a2'],'products':[1277,1854,9132,1277,1853,9332,]})
customer products
0 a1 1277
1 a1 1854
2 a1 9132
3 a2 1277
4 a2 1853
5 a2 9332
def fun(h):
return list(h)
df2=df2.groupby('customer').agg(fun)
df2 after groupby
products
customer
a1 [1277, 1854, 9132]
a2 [1277, 1853, 9332]
validcustomer=[]
for x in range(len(df2)):
temp=df2.iloc[x].products
c=0
for y in range(len(df)):
if df.iloc[y].product1 in temp and df.iloc[y].product2 in temp:
c=1
validcustomer.append(False)
break
if c==0:
validcustomer.append(True)
df2["validity"]=validcustomer
df2 after this
products validity
customer
a1 [1277, 1854, 9132] False
a2 [1277, 1853, 9332] True
what i did was complete brute force very efficient codes can also be written.
i might have been easy if you have explained your problem better and given dome code to regenerate the dataframe.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…