Resolved: Match unique group from another dataframe – one to many match

In this post, we will see how to resolve Match unique group from another dataframe – one to many match

Question:

df1 is the information in the box. Each box has a different volume size The volume of box A is 30, B is 25, etc.
df2 is the information of the product Each product has a different amount
insert “box id” column to df2 to find and match the appropriate box id of df1. Like the data frame at the bottom.
Add the amount(df2) in order from the top to get close to the each box volume(df1) but not exceed the each box
For example, since the first box volume of df1 is 30, so it can contain first row product(amount 10) of df2 with the second row(amout 5) and the third(amount 13) is equal to 30 because 10+5+13 = 28. (However, if you add up to the 4th row, 10+5+13+15 = 43, which exceeds 30
Python is still a beginner, so please give me advice from many experts. It’s a very important task for me.
match the appropriate box id of df1 in the box id column in df2.

Best Answer:

One way using pandas.cut
Output:

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com