Python解决Dataframe中的复杂库存

问题描述

需要您帮助解决订购库存问题。这只有 4 个项目,但真正的 DataFrame 有 10,000 个项目。

df = pd.DataFrame(data)

    Inventory count Batch size  Store A needs   Store B needs   Store C needs    Total requires:    Actually requires: 
Buckets         198      20             63               18             104           185                   220
Candy Bars      876     100            567              435             673          1675                  1800
Coke (cans)    1759       6           1212              758             836          2806                  2814
Masks (Boxes)  2000    1000            333              444             555          1332                  3000
  • df['Inventory count'] 是我手头有多少库存
  • df['Batch size'] 是我每次必须以倍数分配的数量

桶行我手头有 198 个桶。所需的 3 家商店总数为 185,但由于批量大小,63->80、18->20、104->120、80+20+120 = 220。我该如何分配库存?

Candy Bars 和 Coke 的库存数量都小于 df['Actually requires:'],我如何根据需求排名分配它们?

或者,如果有更好的解决方案,我愿意接受建议

这是数据框: enter image description here

我需要你的帮助,谢谢你的帮助。

解决方法

如果上面的例子过于简单,我会说你应该搜索库存和/或供应链管理的分配策略——这意味着需要更多的商店和/或库存分配策略。

我只是返回一个字典,但如果您想要或需要,您可以将数据写回数据帧中的列或创建新数据帧或直接将数据发送到文件。一切都取决于您需要做什么。

import math

def allocation(x):
    max_units_available = math.floor(x['Inventory count'] / x['Batch size'])
    sa_units_need = math.ceil(x['Store A needs'] / x['Batch size'])
    sb_units_need = math.ceil(x['Store B needs'] / x['Batch size'])
    sc_units_need = math.ceil(x['Store C needs'] / x['Batch size'])

    allocation_needs = [sa_units_need,sb_units_need,sc_units_need]
    # print(allocation_needs)
    # print(max_units_available,tota_untis_need)
    if max_units_available >= sum(allocation_needs):
        return dict(zip(store_list,allocation_needs))
    else:
        #allocation goes in order until all units are allocated
        #you can pass as many allocations strategies as you wish,you just need to code them
        #and work them into your code
        store_list = ['Store A received','Store B received','Store C received']
        allocation_received = [0]*len(allocation_needs)
        inv_sum = 0

        for i in range(0,len(allocation_needs)):
            inv_sum = inv_sum + allocation_needs[i]
            if max_units_available < inv_sum:
                allocation_received[i] = max_units_available
                break
            else:
                allocation_received[i] = allocation_needs[i]
            max_units_available = max_units_available - allocation_needs[i]
        # print(dict(zip(store_list,allocation_received)))
        return dict(zip(store_list,allocation_received))
    
    df.apply(lambda x: allocation(x),axis=1)

输出:

Buckets            {'Store A received': 4,'Store B received': 1,'Store C received': 4}
CandyBars          {'Store A received': 6,'Store B received': 2,'Store C received': 0}
Coke(cans)      {'Store A received': 202,'Store B received': 91,'Store C received': 0}
Masks(boxes)       {'Store A received': 1,'Store C received': 0}