I am new learner for python. I have two dataframe loaded from xlsx file in python, and tried to get the value to table 2 from table 1
Table 1:
Product ID | Inventory Receipt Date | Age Days | Quantity |
---|---|---|---|
AA12345678 | Jan 21, 2022 | 120 | 400 |
AA12345678 | Jan 30, 2022 | 111 | 100 |
AA12345678 | Jan 31, 2022 | 110 | 20 |
BB12345678 | Jan 21, 2022 | 120 | 120 |
BB12345678 | Feb 1, 2022 | 109 | 100 |
Table 2:
Location Code | Product ID | Required Quantity |
---|---|---|
ABCD001 | AA12345678 | 100 |
ABCD001 | AA12345678 | 401 |
ABCD002 | AA12345678 | 19 |
EFGH001 | BB12345678 | 200 |
EFGH002 | BB12345678 | 20 |
Expected Result:
Location Code | Product ID | Required Quantity | Age days 1 | Age days 2 | Age days 3 |
---|---|---|---|---|---|
ABCD001 | AA12345678 | 100 | 120 | ||
ABCD001 | AA12345678 | 401 | 120 | 111 | 110 |
ABCD002 | AA12345678 | 19 | 110 | ||
EFGH001 | BB12345678 | 200 | 120 | 109 | |
EFGH002 | BB12345678 | 20 | 109 |
The rule of product distribution is first come first served. For example, 'Location Code' ABCD001 require 100 qty. ('Product ID'= 'AA12345678')on row 2 in table 2. It will distribute 100 qty. ('Product ID'='AA12345678') to row 1 and get the 'Age days' to table 2. When the 'Quantity' on row 2 in table 1 is empty, it will lookup row 3 (with same 'Product ID'=AA12345678). The total number of 'Quantity' in table 1 is same as table 2.
I tried to use df2.merge(df1[['Product ID', 'Age Days']], 'left'), but 'Age Days' cannot merge to df2. And tried to use map function (df2['Age Days'] = df2['Product ID'].map(df1.set_index('Product ID')['Age Days'])), but it occur error "uniquely value".
Issue: 'Product ID' is non-unique for lookup/map/merge. How could get all results or its index by lookup/map/merge/other method? In that case, I need to set a flag called "is_empty" for checking, if "is_empty" == yes: I need to get the value from next matched row
I know that case is complex, could you let me know the way to solve it? I am confusing what keywords should I use to study for it. Thank you.