簡體   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