遍历字典并使用lambda函数计算值

时间:2020-11-09 11:02:30

标签: python pandas dataframe lambda

我尝试遍历df2并将est7wks的值乘以字典product_mean中的相应均值(如果产品线是全局线),否则返回{{1} }值。

forecast

我的代码。

product_mean = {
    'GROCERY': 1.94,
    'DRINKS': 1.57,
    'PHONES': 2.08
}

df2 = pd.DataFrame([
    {
        'Description': 'cornflakes', 
        'department': 'GROCERY',
        'est_7wks': 2043,
        'Product_Line': 'Global-Line',
        'forecast': 'pending'
    },
    {
        'Description': 'coca-cola',
        'department': 'DRINKS',
        'est_7wks': 10500,
        'Product_Line': 'Global-Line',
        'forecast': 'pending'
    },
    {
        'Description': 'iphone 11',
        'department': 'PHONES',
        'est_7wks': 140,
        'Product_Line': 'Diamond-Line',
        'forecast': 'pending'
    }
])

它有效,但仅返回字典项def section_dep(product_mean, department, est_7wks, Product_Line, forecast): for k,v in product_mean.items(): if department == k and Product_Line = 'Diamond-Line': return est_7wks*v else: return forecast df['forecast'] = df.apply( lambda x: section_dep( product_mean, x['department'], x['est_7wks'], x['Product_Line'], x['forecast'] ), axis=1 ) 中第一个的变量。其他部门,例如GROCERYDRINKS都没有回报。

2 个答案:

答案 0 :(得分:0)

您可以使用np.select来实现。使用此方法,您无需遍历DataFrame中的行,这将更快。

# Make a boolean mask for "Diamond-Line"
diamond_mask = df2["Product_Line"].eq("Diamond-Line")

# Create a conditions list from product_mean
conditions = [(diamond_mask & df2["department"].eq(k)) for k in product_mean]

# Create a choices list from product_mean
choices = [df2["est_7wks"] * v for k, v in product_mean.items()]

# Use np.select, with a default of forecast
df2["res"] = np.select(condlist=conditions, choicelist=choices, default=df2["forecast"])

输出:

Out[14]:   Description department  est_7wks      Product_Line forecast      res
         0  cornflakes    GROCERY      2043  Global-Line-Line  pending  pending
         1   coca-cola     DRINKS     10500  Global-Line-Line  pending  pending
         2   iphone 11     PHONES       140      Diamond-Line  pending    291.2

答案 1 :(得分:0)

section_dep函数应为:

def section_dep(product_mean, department, est_7wks, Product_Line, forecast):
    if Product_Line == 'Global-Line-Line' and department in product_mean:
        return est_7wks * product_mean[department]
    else:
        return forecast