I want to go to as few stores as possible to get my products. How do I do this? I have a list of stores that carry a specific product.
wanted_Products = pd.DataFrame({'p':[1,2,3,4,5,6,7]})
stores = pd.DataFrame({'Store': np.repeat(np.arange(1,5),4),
'Product': [1,2,3,5,0,2,3,4,0,6,7,8,0,1,2,6]})
# return 1 if the Product is wanted
stores['Wanted'] = stores.Product.isin(wanted_Products.p).values.astype(int)
Store Product Wanted
0 1 1 1
1 1 2 1
2 1 3 1
3 1 5 1
4 2 0 0
5 2 2 1
6 2 3 1
7 2 4 1
8 3 0 0
9 3 6 1
10 3 7 1
11 3 8 0
12 4 0 0
13 4 1 1
14 4 2 1
15 4 6 1
# Group products per store and calculate how many wanted products are in a store
w = stores.groupby('Store', as_index=False).agg(list)
w['Number_wanted'] = stores.groupby('Store', as_index=False)['Wanted'].sum().agg(list)['Wanted']
Store Product Wanted Number_wanted ?Products_wanted?
0 1 [1, 2, 3, 5] [1, 1, 1, 1] 4 [1,2,3,5]
1 2 [0, 2, 3, 4] [0, 1, 1, 1] 3 [2,3,4]
2 3 [0, 6, 7, 8] [0, 1, 1, 0] 2 [6,7]
3 4 [0, 1, 2, 6] [0, 1, 1, 1] 3 [1,2,6]
How do I get the Products I want in a new column (Products_wanted) without the non wanted products? when I use isin() I only get true/false (1/0 if I use astype(int)) not the actual numbers.