Reputation: 29
Here is what I want to do.
I have two Pandas DataFrames
List of items and Qty to buy
ITEM, QTY
A,1
B,2
C,3
Locations at which each ITEM is available. One item can be available at multiple locations.
ITEM, LOCATION
A, LOC1
B, LOC2
B, LOC3
C, LOC4
C, LOC5
I need to find out all the possible combinations the ITEMS can be purchased
Combination 1
A, LOC1
B, LOC2
C, LOC4
Combination 2
A, LOC1
B, LOC2
C, LOC5
Combination 3
A, LOC1
B, LOC3
C, LOC4
Combination 4
A, LOC1
B, LOC3
C, LOC5
Upvotes: 2
Views: 131
Reputation: 164843
You can use itertools.product
. For a variable number of variables, you can use a dictionary.
from itertools import product
d = df2.groupby('ITEM')['LOCATION'].apply(list).to_dict()
dfs = {i: pd.DataFrame({'LOCATION': j}).assign(ITEM=d.keys())
for i, j in enumerate(product(*d.values()), 1)}
print(dfs)
{1: LOCATION ITEM
0 LOC1 A
1 LOC2 B
2 LOC4 C,
2: LOCATION ITEM
0 LOC1 A
1 LOC2 B
2 LOC5 C,
3: LOCATION ITEM
0 LOC1 A
1 LOC3 B
2 LOC4 C,
4: LOCATION ITEM
0 LOC1 A
1 LOC3 B
2 LOC5 C}
Upvotes: 1