I have the following dataframes
print(sellers)
cost product seller
0 2000 soft drinks seller1
1 4000 soft drinks seller2
print(customers)
price customer product
0 4100 customer1 soft drinks
1 4250 customer2 soft drinks
I want to iterate over each row to produce all possible matches between customers and sellers in the similar to the following output:
option1 cost option2 cost2
0 (customer1, seller1) 2000 (customer2, seller2) 4000
1 (customer1, seller1) 2000 (customer2, seller1) 2000
2 (customer1, seller2) 4000 (customer2, seller2) 4000
3 (customer1, seller2) 4000 (customer2, seller1) 2000
my code did not seem to work for what I want.
for i1, row1 in customers.iterrows():
for i2, row2 in sellers.iterrows():
print(row1['customer'],row2['seller'],row2['cost'])
Try this (17ms to run):
d1 = [{'cost': 2000, 'product': 'soft drinks', 'seller': 'seller1' }, {'cost': 4000, 'product': 'soft drinks', 'seller': 'seller2'}]
df1 = pd.DataFrame(d1)
d2 = [{'price': 4100, 'customer': 'customer1', 'product': 'soft drinks' }, {'price': 4250, 'customer': 'customer2', 'product': 'soft drinks'}]
df2 = pd.DataFrame(d2)
l = list(itertools.product(df2.customer, df1.seller))
l1 = list(itertools.combinations(l,2))
z = []
for i in range(len(l1)):
x = l1[i][0]
x_cost = int(df1[df1.seller==x[1]]['cost'])
y = l1[i][1]
y_cost = int(df1[df1.seller==y[1]]['cost'])
z.append([x,x_cost, y, y_cost])
print(pd.DataFrame(z, columns = ['option1','cost','option2','cost2']))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.