gpt4 book ai didi

python - 需要帮助将此函数应用于 Pandas 数据框列

转载 作者:行者123 更新时间:2023-12-01 08:40:24 24 4
gpt4 key购买 nike

我正在尝试通过查找 sku 的父 asin 并计算满足某些条件的行来查找 allinv_styles 数据框中产品的 sku,但我不知道我在做什么,我将非常感谢您帮助。

我收到错误消息:“ValueError:只能将大小为 1 的数组转换为 Python 标量。”

我有两个数据框 adgroups_df 和 allinv_styles。

adgroups_df 有一个名为“广告组”的列,其中包含产品的 sku。

SKU 特定于产品的款式和尺寸。像黑色的小号。父 asin 可以有许多 sku 和样式。我正在尝试编写一个函数来计算广告组所代表的样式的缺货百分比。

我的思考过程是:

  • 查找广告组的父级
  • id 广告组样式
  • 查找该行的父行
  • 计算该样式在该父级 asin 中有多少行
  • 计算有多少行有库存<0
  • 计算 oos %
  • 返回 oos %
  • 通过将函数应用于每个广告组列来创建新列

这是我的意大利面条代码:

def calc_style_OOS(adgroups):
for sku in adgroups:
# find parent asin of ad group sku
parentasin = allinv_styles.loc[(allinv_styles['sku'] == sku)]['(Parent) ASIN'].item()

# I tried to print here to debug...
print(parentasin)

# find style of sku
style = allinv_styles.loc[(allinv_styles['sku'] == sku)]['style'].item()

# how many variations does this style have?
total_variations = len(allinv_styles.loc[(allinv_styles['(Parent) ASIN'] == parentasin) &
(allinv_styles['style'] == style)])

# how many of these rows have 0 stock?
oos_variations = len(allinv_styles.loc[(allinv_styles['(Parent) ASIN'] == parentasin) &
(allinv_styles['style'] == style) &
(allinv_styles['afn-fulfillable-quantity'] < 0)])

# caclulate oos %

if total_variations == 0:
return 0
else:
oos = oos_variations/total_variations
return oos

adgroups_df['OOS %'] = adgroups_df['Ad Group'].apply(calc_style_OOS)

深入的错误消息:

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-37-7ba9d94d5581> in <module>()
----> 1 adgroups_df['OOS %'] = adgroups_df['Ad Group'].apply(calc_style_OOS)

~\Anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds)
2549 else:
2550 values = self.asobject
-> 2551 mapped = lib.map_infer(values, f, convert=convert_dtype)
2552
2553 if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/src/inference.pyx in pandas._libs.lib.map_infer()

<ipython-input-36-ac54497ca2ef> in calc_style_OOS(adgroups)
14 for sku in adgroups:
15 # find parent asin of ad group sku
---> 16 parentasin = allinv_styles.loc[(allinv_styles['sku'] == sku)]['(Parent) ASIN'].item()
17 # I tried to print here to debug...
18 print(parentasin)

~\Anaconda3\lib\site-packages\pandas\core\base.py in item(self)
717 """
718 try:
--> 719 return self.values.item()
720 except IndexError:
721 # copy numpy's message here because Py26 raises an IndexError

ValueError: can only convert an array of size 1 to a Python scalar

最佳答案

如果我正确理解了问题,请更改此:

def calc_style_OOS(adgroups):
for sku in adgroups:

对此:

def calc_style_OOS(sku):

Series.apply 正在按元素应用函数,您不需要 calc_style_OOS 中的循环。

如果您想在 calc_style_OOS 中使用 allinv_styles 作为参数传递给 apply:

adgroups_df['OOS %'] = adgroups_df['Ad Group'].apply(calc_style_OOS, args=(allinv_styles,))

但是,我认为您应该为 (Parent) ASINstyletotal_variationsoos_variations 创建 4 个临时列code> 而不是在自定义 apply 函数中计算每一项。

示例(未经测试)

# Map (Parent) ASIN
adgroups_df['(Parent) ASIN'] = adgroups_df.sku.map(dict(zip(allinv_styles.sku, allinv_styles['(Parent) ASIN'])))

# Map style
adgroups_df['style'] = adgroups_df.sku.map(dict(zip(allinv_styles.sku, allinv_styles.style)))

# Get variation counts
group_cols = ['(Parent) ASIN', 'style']
total_variations = allinv_styles[group_cols].groupby(group_cols).size()
oos_variations = allinv_styles['afn-fulfillable-quantity'] < 0)][group_cols].groupby(group_cols).size()

# Calculate %, map back to adgroups_df
oos_percents = oos_variations / total_variations
oos_percents = oos_percents.where(oos_percents != np.inf, 0)
adgroups_df = adgroups_df.join(oos_percents, on=group_cols)

关于python - 需要帮助将此函数应用于 Pandas 数据框列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53531196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com