繁体   English   中英

pandas df 迭代一个 df 列 qty 并以 fifo 为基础从另一个 df 列分配 qty

[英]pandas df iterate over one df column qty and allocate qty from another df column with fifo basis

感谢您的时间!!!

我有 2 个 dfs,第一个有一个位置项目和一个数量。

data1 = [['0', 'L1','AAA',681.47],['1', 'L1','AAA',1],['2', 'L1','AAA',576],['3', 'L1','AAA',387],['4', 'L1','AAA',581],['5', 'L2','AAA',28],['6', 'L3','AAA',44],['7', 'L3','AAA',85] ]
df1 = pd.DataFrame(data2, columns=['index','location','Item','Qty'])

第二个 df 有项目、位置、ID 和数量。

data2 = [['0','AAA', 'L1','ID1',5],['1','AAA', 'L1','ID2',7.5],['2','AAA', 'L1','ID3',750],['3','AAA', 'L1','ID4',28.41],['4','AAA', 'L2','ID5',22.7],['5','AAA', 'L2','ID6',500.7] ]
df2 = pd.DataFrame(data, columns=['index', 'Item','location','ID','Qty'])

我需要从 df2 分配 df1 的数量。 取决于位置和项目编号。 并且需要根据分配在新的df中创建一个名为“ID”的新列(目的是识别相关的ID)。

我想要的结果如下, 红色高光输出就够了

我的尝试如下,但我认为必须有一种更简单的方法,我也无法得到确切的结果。

TR_Qty = df2['Qty'].tolist()
mylist = []
list2 = []
df1 = pd.DataFrame()
ind =0 
consum_bal=0
allocInv =0
age_bal =0
for item in df1['key']:
    age_bal = df1.loc[ind,'Total Quantity']
    if consum_bal >0:
        if consum_bal <= age_bal:
            allocInv = consum_bal
            age_bal = age_bal - consum_bal
            consum_bal = 0
            print('1allocated inv',allocInv,'age_bal',age_bal,'consum_bal',consum_bal)
        else:
            allocInv = age_bal
            age_bal = 0 #check
            consum_bal = consum_bal - allocInv
            print('2allocated inv',allocInv,'age_bal',age_bal,'consum_bal',consum_bal)
    else:
        for i in TR_Qty:
            if consum_bal < 0:
                try:
                    del TR_Qty[0]
                except:
                    continue
            if i <= age_bal:
                age_bal = age_bal - i
                allocInv = i
                consum_bal = 0
                print('3allocated inv',allocInv,'age_bal',age_bal,'consum_bal',consum_bal)
            elif age_bal >0:
                allocInv = age_bal
                consum_bal = i - allocInv
                age_bal =0
                print('4allocated inv',allocInv,'age_bal',age_bal,'consum_bal',consum_bal)
            else:
                try:
                    del TR_Qty[0]
                except:
                    continue
        try:
            del TR_Qty[0]
        except:
            continue
        print(TR_Qty)
    ind +=1

给定

# df1

  location Item     Qty
0       L1  AAA  681.47
1       L1  AAA    1.00
2       L1  AAA  576.00
3       L1  AAA  387.00
4       L1  AAA  581.00
5       L2  AAA   28.00
6       L3  AAA   44.00
7       L3  AAA   85.00

# df2

  Item location   ID     Qty
0  AAA       L1  ID1    5.00
1  AAA       L1  ID2    7.50
2  AAA       L1  ID3  750.00
3  AAA       L1  ID4   28.41
4  AAA       L2  ID5   22.70
5  AAA       L2  ID6  500.70

正在做:

df = df1.merge(df2, on=['Item', 'location'])
print(df)

# Output:

   location Item   Qty_x   ID   Qty_y
0        L1  AAA  681.47  ID1    5.00
1        L1  AAA  681.47  ID2    7.50
2        L1  AAA  681.47  ID3  750.00
3        L1  AAA  681.47  ID4   28.41
4        L1  AAA    1.00  ID1    5.00
5        L1  AAA    1.00  ID2    7.50
6        L1  AAA    1.00  ID3  750.00
7        L1  AAA    1.00  ID4   28.41
8        L1  AAA  576.00  ID1    5.00
9        L1  AAA  576.00  ID2    7.50
10       L1  AAA  576.00  ID3  750.00
11       L1  AAA  576.00  ID4   28.41
12       L1  AAA  387.00  ID1    5.00
13       L1  AAA  387.00  ID2    7.50
14       L1  AAA  387.00  ID3  750.00
15       L1  AAA  387.00  ID4   28.41
16       L1  AAA  581.00  ID1    5.00
17       L1  AAA  581.00  ID2    7.50
18       L1  AAA  581.00  ID3  750.00
19       L1  AAA  581.00  ID4   28.41
20       L2  AAA   28.00  ID5   22.70
21       L2  AAA   28.00  ID6  500.70

然后应用您的复杂逻辑的 rest ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM