优先级矩阵

Priority matrix

我有以下任务:

我确实有一个 table 如下所示:

如您所见,我有 10 列。

我需要创建一个新列“WhseFrom”,它将 return 基于此类的 7 个(AH、AYC、MAEG、MAR、ROT、WITZ、BUD)列之一的值名称来自此背后想法的优先级条件 table:

然后,新列“Pull2”中应包含的值应基于条件。

例如,当商店 == BUD 时,跟踪器数据应在 Pull 列高于 0 时检查,如果 WHSE== WITZ 中的数量足够(等于或高于)Pull 列中的数量,如果是,它应该中断搜索和 return Pull 值,但如果不是,tracker 函数应该继续并检查 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 上开发它的建议。

对于数据操作,按照示例创建下面的“table 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

因此,预期要 returned 的列是这个 WHSEFrom,如果对于 store==BUD 我们在 Pull 列中确实有足够的数量并且数量首先在WITZ 列,如果是,它将中断并 return 此 WHSE WITZ 的名称在此列 WhseFrom 上。如果没有,它将根据 PULLWH 枢轴 table(p1>p2>p3>p4>p5).

中的优先规则检查其他 6 个仓库是否有最小数量。

非常感谢任何帮助。

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

试试这个:

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