I am very new to python or pandas coding. so I am kind of struck here and any input is appreciated. I have two df, individually ordered based on a criteria. df1 : list of orders with quantity df2 : list of inventories with quantity and date available. quantity necessarily not to equal to order quantity.
I need to pop first order in df1 and keep popping inventory in df2 until order quantity satisfied and also maintain, how many inventory I took to fulfill the order
Any help would be greatly appreciated
Example
df 1:
order1 5
order2 4
order3 5
df 2: inventories
inven1 7 07/06/2020
inven2 7 07/10/2020
my final output should look like this.
inven1 | 5 | 07/06/2020 | order1
inven1 | 2 | 07/06/2020 | order2
inven2 | 2 | 07/10/2020 | order2
inven2 | 5 | 07/10/2020 | order3
@Ian Thanks for your answer it works perfectly fine, only problem I am having is, these inventory and order have to be matched based on a metaID, inventories in metaId should be applied to order of the same metaID.
I tried looping through the metaID's and performing the order inventory match as below. I get the result I wanted, but the biggest problem is it take 20 mins for a data size for just 2K rows.
I tried solving it by sorting the orders and inventories by metaID, and passing the entire dataframe. it works when Order and inventories have exact matching count,
df 1:
metaId1 order1 5
metaId1 order2 4
metaId1 order3 5
metaId2 order4 10
df 2: inventories
metaId1 inven1 7 07/06/2020
metaId1 inven2 7 07/10/2020
metaId2 inven3 10 07/10/2020
my final output is all fine.
metaId1 | inven1 | 5 | 07/06/2020 | order1
metaId1 | inven1 | 2 | 07/06/2020 | order2
meatId1 | inven2 | 2 | 07/10/2020 | order2
metaId1 | inven2 | 5 | 07/10/2020 | order3
metaId2 | inven3 | 10 | 07/10/2020 | order4
but crossover between metaId's happened when the inventory is excess or short, cause we are matching based on the index. how can you match based on metaId then by index within. the for loop solution I had takes forever for huge data and i know thats not the right solution. These order matching under each metaId can happen in parallel and results can be merged. That how I did it with for loop. I dunno how to parallelize them in python/pandas or dunno is there a better solution than parallel computation. It seems like I need to apply groupBy-appLy-combine concept, but the I see examples of that concept with single dataframe and apply some logic for that row, then combine it. My issue is, I have two dataframes, have to group by and apply the "combine-first()" solution to that group and combine the results from each group. I not sure how to do it. can you help me on this.
current problem
df 1:
metaId1 order1 5
metaId1 order2 4
metaId1 order3 5
metaId2 order4 10
df 2: inventories
metaId1 inven1 7 07/06/2020
metaId1 inven2 5 07/10/2020
metaId2 inven3 10 07/10/2020
final output : --> not right
metaId1 | inven1 | 5 | 07/06/2020 | order1
metaId1 | inven1 | 2 | 07/06/2020 | order2
meatId1 | inven2 | 2 | 07/10/2020 | order2
metaId1 | inven2 | 3 | 07/10/2020 | order3
metaId2 | inven3 | 2 | 07/10/2020 | order3 -->crossover
metaId2 | inven3 | 8 | 07/10/2020 | order4
expected output :
metaId1 | inven1 | 5 | 07/06/2020 | order1
metaId1 | inven1 | 2 | 07/06/2020 | order2
meatId1 | inven2 | 2 | 07/10/2020 | order2
metaId1 | inven2 | 3 | 07/10/2020 | order3
metaId1 | short | 2 | 07/10/2020 | order3-->short by 2 inven to fulfill order
metaId2 | inven3 | 10 | 07/10/2020 | order4