【发布时间】:2019-05-01 01:18:27
【问题描述】:
我正在尝试通过查找父 asin 的 sku 并计算满足某些条件的行来在我的 allinv_styles 数据框中查找产品的 sku,但我不知道自己在做什么,非常感谢您帮助。
我收到错误消息:“ValueError:只能将大小为 1 的数组转换为 Python 标量。”
我有两个数据框 adgroups_df 和 allinv_styles。
adgroups_df 有一个名为“广告组”的列,其中包含产品的 sku。
SKU 特定于产品的样式和尺寸。喜欢黑色小。一个父 asin 可以有许多 sku 和样式。我正在尝试编写一个函数来计算广告组所代表的样式的缺货百分比。
我的思考过程是:
- 查找广告组的父类
- id 广告组样式
- 查找该行的父行
- 计算该样式在该父 asin 中有多少行
- 计算有多少行存货
- 计算 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
【问题讨论】:
标签: python pandas dataframe anaconda