熊猫-如果值存在则添加新列,然后添加布尔值

时间:2020-03-06 12:02:21

标签: pandas

我有一个包含产品列表的数据框

products
prod_a
prod_b
prod_c

我有另一个数据框,其帐单详细信息如customer_namedate_of_saleproduct_purchased

我正在尝试创建一个新的数据框,以便添加一个新列,该列是产品数据框中所有产品的列表,并且如果客户购买了该产品,则这些布尔值将为布尔值。

数据框的示例视图:

customer_name, date_of_sale,product_purchased
cust_a,2020/01/01,prod_a
cust_b,2020/01/01,prod_a, prod_c
cust_c,2020/01/01,prod_a
cust_d,2020/01/01,prod_b
cust_e,2020/01/01,prod_c

我的新数据框如下所示:

customer_name, date_of_sale,product_purchased,prod_a,prod_b,prod_c
cust_a,2020/01/01,prod_a,1,0,0
cust_b,2020/01/01,prod_a, prod_c,1,0,1
cust_c,2020/01/01,prod_a,1,0,0
cust_d,2020/01/01,prod_b,prod_c,0,1,1
cust_e,2020/01/01,prod_c,0,0,1

如果这是一小部分产品,我可以手动添加它们,但是我的列表中大约有300种产品,所以我想知道如何执行循环并将这些产品添加为列,然后检查其值每个完成的销售都存在。

0 个答案:

没有答案