使用值

时间:2017-10-14 14:03:06

标签: python pandas

我有以下数据框

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

我想迭代每一行,以产生客户和卖家之间所有可能的匹配,类似于以下输出:

    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

我的代码似乎不能满足我的需求。

for i1, row1 in customers.iterrows():
    for i2, row2 in sellers.iterrows():
       print(row1['customer'],row2['seller'],row2['cost'])

1 个答案:

答案 0 :(得分:1)

试试这个(17ms运行):

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']))