如何用Python基于两个DataFrame设置while循环实现仓库货位分配
仓库货位分配实现方案
需求说明
为需求33托盘的Tito's Vodka分配货位,需遵循以下规则:
- 优先按
Slot_Type_Sequence(5种类型)排序分配 - 同类型货位内按
Label顺序分配 - 通过
while循环实现,凑够总容量为33的前3个货位
数据结构假设
sorted_location DataFrame(示例)
包含货位标签、类型序列、容量信息:
| Label | Slot_Type_Sequence | Capacity |
|---|---|---|
| A01 | 1 | 12 |
| A02 | 1 | 10 |
| A03 | 1 | 11 |
| A04 | 2 | 15 |
| ... | ... | ... |
demand DataFrame(示例)
| Product | Pallet_Demand |
|---|---|
| Tito's Vodka | 33 |
代码实现
import pandas as pd # 加载数据(替换为实际数据加载逻辑) sorted_location = pd.read_csv("sorted_location.csv") demand = pd.read_csv("demand.csv") # 提取目标产品需求 target_product = "Tito's Vodka" required = demand.loc[demand["Product"] == target_product, "Pallet_Demand"].iloc[0] # 初始化分配变量 remaining_pallets = required assigned = [] current_pos = 0 # while循环分配货位 while remaining_pallets > 0 and current_pos < len(sorted_location): current_slot = sorted_location.iloc[current_pos] # 计算本次可分配托盘数 assign_num = min(current_slot["Capacity"], remaining_pallets) # 记录分配明细 assigned.append({ "Product": target_product, "Label": current_slot["Label"], "Slot_Type_Sequence": current_slot["Slot_Type_Sequence"], "Assigned_Pallets": assign_num, "Remaining_Pallets": remaining_pallets - assign_num }) # 更新剩余需求和货位指针 remaining_pallets -= assign_num current_pos += 1 # 转换为结果DataFrame assignment_result = pd.DataFrame(assigned) print(assignment_result)
期望输出
Product Label Slot_Type_Sequence Assigned_Pallets Remaining_Pallets 0 Tito's Vodka A01 1 12 21 1 Tito's Vodka A02 1 10 11 2 Tito's Vodka A03 1 11 0
内容的提问来源于stack exchange,提问作者Masita
相关产品推荐
相关产品推荐

