希望我对您的问题理解正确:您可以对数据帧A进行分组并求和各数量A,然后遍历数据帧B的行,从总量A中减去数量B:
df_A = df_A.groupby(["PO", "ItemCode"], as_index=False).agg({"Invoice": "first", "QtyA": "sum"})
df_B = df_B.set_index(["PO", "ItemCode"])
out = []
for rowA in df_A.itertuples():
if (rowA.PO, rowA.ItemCode) not in df_B.index:
out.append(
{
"PO": rowA.PO,
"ItemCode": rowA.ItemCode,
"Invoice": rowA.Invoice,
"QtyC": rowA.QtyA,
"Ref": None,
"Line": 0,
}
)
continue
qty_remaining = rowA.QtyA
for rowB in df_B.loc[(rowA.PO, rowA.ItemCode)].itertuples():
if qty_remaining - rowB.QtyB >= 0:
n = rowB.QtyB
else:
n = qty_remaining
out.append(
{
"PO": rowA.PO,
"ItemCode": rowA.ItemCode,
"Invoice": rowA.Invoice,
"QtyC": n,
"Ref": rowB.Ref,
"Line": rowB.Line,
}
)
qty_remaining -= n
if qty_remaining == 0:
break
if qty_remaining > 0:
out.append(
{
"PO": rowA.PO,
"ItemCode": rowA.ItemCode,
"Invoice": rowA.Invoice,
"QtyC": qty_remaining,
"Ref": None,
"Line": 0,
}
)
out = pd.DataFrame(out)
print(out)
这段代码会输出以下内容:
PO ItemCode Invoice QtyC Ref Line
0 1001 ITEMA 2001 2 8986.0 15
1 1001 ITEMB 2001 1 8986.0 16
2 1002 ITEMB 2002 4 NaN 0
3 1003 ITEMA 2003 7 8987.0 8
4 1003 ITEMA 2003 2 NaN 0
5 1004 ITEMA 2004 1 8415.0 19
6 1005 ITEMB 2005 3 NaN 0
7 1006 ITEMA 2006 2 8469.0 1
8 1006 ITEMA 2006 1 8253.0 12
9 1006 ITEMA 2006 2 NaN 0