Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

python - I want to check values in two columns at same index should not occur in rows of a group dataset

I have two tables: df_exclude having products that should not come together to the same customer i.e product1 and product2 at 0 index should not be given to the same customer:

product1 product2
565043 5649965
5649585 5649910
5649585 5649921
5649607 5649931
5649607 5649929
question from:https://stackoverflow.com/questions/65948211/i-want-to-check-values-in-two-columns-at-same-index-should-not-occur-in-rows-of

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...