优先级矩阵

2024-09-30 14:29:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我的任务如下:

我确实有一张桌子,如下所示: enter image description here

如你所见,我有10列

我需要创建一个新的列“WhseFrom”,它将返回7个(AH、AYC、MAEG、MAR、ROT、WITZ、BUD)列中的一个列的值名,这些列基于此表背后的思想产生的优先级条件:

enter image description here

然后,这个新列“Pull2”上应该包含的值应该基于条件

例如,当store==BUD时,跟踪器数据应检查while Pull列是否大于0,如果WHSE==WITZ中的数量足够(等于或高于)Pull列中的数量,如果是,则应中断搜索并返回Pull值,但如果不是,跟踪器功能将继续,因此检查WHSE==ROT中的数量,如果仍然不够,则继续并检查优先级列列表中的后续WHSE

对于所有其他商店=='WHSE',也必须执行此场景

如有任何想法或建议,将不胜感激

我试图创建以下代码:

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

但正如您所看到的,在跟踪所涉及商店的第一个条件后,仍然缺少可能性

请,我需要关于如何在Python上开发它的建议

对于数据操作,请按照以下示例创建“表a”:

import pandas as pd
tabela = [ (8489,'BUD', 175,0,0,0,0,0,1522,0 )]
tabeladf = pd.DataFrame(tabela)
tabeladf

tabeladf.columns = ['PNO','store', 'Pull', 'ADH','AYC', 
'MAEG','MAR','ROT', 'WITZ','BUD' ]
tabeladf

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

因此,预期返回的列是WHSEFrom,它将在每一行检查store==BUD的Pull列中是否有足够的数量,数量first all在WITZ列中,如果是,它将在WHSEFrom列中中断并返回该WHSE WITZ的名称。如果没有,它将检查其他6个仓库是否有符合PULLWH数据透视表(p1>;p2>;p3>;p4>;p5)中优先级规则的最小数量

任何帮助都将不胜感激

输出应类似于以下示例中共享的“预期输出表”:

enter image description here

enter image description here


Tags: andstorereturnifpasspullelserow
1条回答
网友
1楼 · 发布于 2024-09-30 14:29:41

试试这个:

priorities = {"ADH": ["WITZ", "ROT", "MAR", "AYC"],
              "AYC": ["WITZ", "ROT"],
              "MAEG": ["ROT", "WITZ"],
              "ROT": ["WITZ", "MAEG", "MAR", "AYC", "ADH"],
              "MAR": ["WITZ", "ROT", "AYC", "ADH"],
              "WITZ": ["ROT", "MAR", "AYC", "ADH"],
              "BUD": ["WITZ", "ROT", "MAR", "AYC", "ADH"]}

def findQty(row):
    #keep only the stores ordered by priority
    stores = row[priorities[row.at["Store"]]]
    #return first store where cumulative sum exceeds pull value
    result = stores[stores.cumsum()>=row.at["Pull"]].index
    if len(result)>0:
        return result[0]
    else:
        return None
        
df["WHSE"] = df.apply(findQty, axis=1)

>>> df[["Store", "WHSE"]]
          Store  WHSE
PNO                   
1084895083   BUD  WITZ
1118952108   AYC   ROT
1119151296   BUD   MAR
1113150401  WITZ   ROT
1118568003  WITZ   AYC
2231065166  MAEG  WITZ
2238179010  MAEG   ROT

相关问题 更多 >