Python pandas基于优先级矩阵实现仓库调拨WhseFrom列开发求助
仓库调拨规则计算实现需求
任务说明
现有含10个字段的数据表,需要新增两个计算字段:
WhseFrom:基于优先级矩阵规则,从AH、AYC、MAEG、MAR、ROT、WITZ、BUD共7个仓库的库存字段中,返回第一个可满足调拨需求的仓库名称Pull2:存储对应仓库的实际调拨数量
规则说明
当store字段取值为BUD且Pull字段值大于0时,优先校验WITZ仓库库存是否≥Pull需求量,满足则终止遍历,返回WITZ作为WhseFrom值;不满足则继续校验ROT仓库库存,仍不满足则按优先级顺序依次校验后续仓库,其余store取值均执行对应优先级的校验逻辑。
现有代码问题
当前编写的逻辑无法覆盖所有优先级校验场景,代码如下:
def findQty(row): while row['store']== 'BUD' and row['Pull'] > 0: if row['Pull'] >= row['ROT']: return row['Pull'] continue else: if row['Pull'] < row['ROT']: return (row['WITZ'] - row['Pull']) break #maybe here include other columns else: pass else: pass while row['store']== 'WITZ' and row['Pull'] > 0: if row['Pull'] >= row['WITZ']: return row['Pull'] continue else: if row['Pull'] < row['WITZ']: return (row['WITZ'] - row['Pull']) break else: pass else: pass while row['store']== 'MAR' and row['Pull'] > 0: if row['Pull'] >= row['WITZ']: return row['Pull'] continue else: if row['Pull'] < row['WITZ']: return (row['WITZ'] - row['Pull']) break else: pass else: pass while row['store']== 'ROT' and row['Pull'] > 0: if row['Pull'] >= row['WITZ']: return row['Pull'] continue else: if row['Pull'] < row['WITZ']: return (row['WITZ'] - row['Pull']) break else: pass else: pass while row['store']== 'MAEG' and row['Pull'] > 0: if row['Pull'] >= row['ROT']: return row['Pull'] continue else: if row['Pull'] < row['ROT']: return (row['WITZ'] - row['Pull']) break else: pass else: pass while row['store']== 'AYC' and row['Pull'] > 0: if row['Pull'] >= row['WITZ']: return row['Pull'] continue elif row['Pull'] < row['WITZ']: return (row['WITZ'] - row['Pull']) break
测试数据构造示例
import pandas as pd tabela = [ (8489,'BUD', 175,0,0,0,0,0,1522,0 )] tabeladf = pd.DataFrame(tabela) tabeladf.columns = ['PNO','store', 'Pull', 'ADH','AYC', 'MAEG','MAR','ROT', 'WITZ','BUD' ] def findQty(row): while row['store']== 'BUD' and row['Pull'] > 0: if row['Pull'] >= row['ROT']: return row['Pull'] continue else: if row['Pull'] < row['ROT']: return (row['WITZ'] - row['Pull']) break #maybe here include other columns else: pass tabeladf['WhseFrom'] = tabeladf.apply(findQty, axis=1) tabeladf
可行实现方案
实现思路
将不同门店对应的仓库校验优先级抽象为可配置的映射字典,无需写重复的分支判断逻辑,遍历对应优先级列表找到第一个满足库存要求的仓库即可,后续规则调整只需要修改映射字典即可。
实现代码
import pandas as pd # 可根据实际优先级矩阵调整每个门店对应的仓库校验顺序 priority_map = { "BUD": ["WITZ", "ROT", "MAR", "MAEG", "AYC", "ADH", "BUD"], "WITZ": ["ROT", "MAR", "MAEG", "AYC", "ADH", "BUD", "WITZ"], "MAR": ["WITZ", "ROT", "MAEG", "AYC", "ADH", "BUD", "MAR"], "ROT": ["WITZ", "MAR", "MAEG", "AYC", "ADH", "BUD", "ROT"], "MAEG": ["ROT", "WITZ", "MAR", "AYC", "ADH", "BUD", "MAEG"], "AYC": ["WITZ", "ROT", "MAR", "MAEG", "ADH", "BUD", "AYC"], "ADH": ["WITZ", "ROT", "MAR", "MAEG", "AYC", "BUD", "ADH"] } def find_whse(row): pull_qty = row["Pull"] # 无调拨需求直接返回空值 if pull_qty <= 0: return pd.Series([None, 0]) # 获取当前门店对应的校验顺序 check_order = priority_map.get(row["store"], []) for whse in check_order: # 找到第一个库存满足需求的仓库 if row[whse] >= pull_qty: return pd.Series([whse, pull_qty]) # 所有仓库都无法满足需求的自定义返回逻辑 return pd.Series([None, 0]) # 测试数据构造 tabela = [ (8489,'BUD', 175,0,0,0,0,0,1522,0 )] tabeladf = pd.DataFrame(tabela) tabeladf.columns = ['PNO','store', 'Pull', 'ADH','AYC', 'MAEG','MAR','ROT', 'WITZ','BUD' ] # 批量计算两个新字段 tabeladf[["WhseFrom", "Pull2"]] = tabeladf.apply(find_whse, axis=1) print(tabeladf)
内容的提问来源于stack exchange,提问作者Yan
相关产品推荐
相关产品推荐

