提问人:Shan Jiang 提问时间:5/18/2023 最后编辑:Shan Jiang 更新时间:5/18/2023 访问量:34
从另一个 DataFrame 更新 DataFrame 的切片,编码为项目编号、因子和周数
updating a slice of dataframe from another dataframe accoding to the item number, Factor, and week number
问:
假设您的数据帧如下所示:
DF1:
商品编号 | 型 | 因素 | 2/4/22 | 2/11/22 | 2/18/22 | 2/25/22 | 3/4/22 | 3/11/22 | 3/18/22 | 3/25/22 | 4/1/22 | 4/8/22 | 4/15/22 | 4/22/22 | 4/29/22 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
123 | 一个 | 售罄预测 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
123 | 一个 | 库存 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
123 | 一个 | 传入 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
345 | B | 售罄预测 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
345 | B | 库存 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
345 | B | 传入 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
678 | C | 售罄预测 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
678 | C | 库存 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
678 | C | 传入 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
G-L | Z | 售罄预测 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Y-1型 | Z | 库存 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
y | Z | 传入 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
DF2:
商品编号 | 型 | 因素 | 1/7/22 | 1/14/22 | 1/21/22 | 1/28/22 | 2/4/22 | 2/11/22 | 2/18/22 | 2/25/22 | 3/4/22 | 3/11/22 | 3/18/22 | 3/25/22 | 4/1/22 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
123 | 一个 | 销售预测 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
345 | B | 销售预测 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 |
如何使用 df2 数据更新 df1 数据,在正确的因子行、模型名称下,并且仅适用于它们的通用周(不更改其他周和型号编号)?
我的预期输出是这样的:
商品编号 | 型 | 因素 | 2/4/22 | 2/11/22 | 2/18/22 | 2/25/22 | 3/4/22 | 3/11/22 | 3/18/22 | 3/25/22 | 4/1/22 | 4/8/22 | 4/15/22 | 4/22/22 | 4/29/22 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
123 | 一个 | 售罄预测 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 0 | 0 | 0 | 0 |
123 | 一个 | 库存 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
123 | 一个 | 传入 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
345 | B | 售罄预测 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 0 | 0 | 0 | 0 |
345 | B | 库存 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
345 | B | 传入 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
678 | C | 售罄预测 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
678 | C | 库存 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
678 | C | 传入 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
G-L | Z | 售罄预测 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Y-1型 | Z | 库存 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
y | Z | 传入 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
我尝试使用 iterrows 获取 df1 中的每一行,并将 comon 周单元格切成与 df2 匹配,但它不起作用,因为链接索引不允许我更改原始数据帧。
如果有人可以帮助我?我真的会欣赏它!
答:
0赞
Timeless
5/18/2023
#1
您可以将 update
与 MultiIndex 一起使用:
cols = ["Item Number", "Model", "Factor"]
df1 = df1.set_index(cols)
df1.update(df2.set_index(cols)) # returns `None`
df1 = df1.reset_index()
输出:
print(df1)
Item Number Model Factor 2/4/22 2/11/22 2/18/22 2/25/22 3/4/22 3/11/22 3/18/22 3/25/22 4/1/22 4/8/22 4/15/22 4/22/22 4/29/22
0 123 A Sell out forecast 5 6 7 8 9 10 11 12 13 0 0 0 0
1 123 A Inventory 0 0 0 0 0 0 0 0 0 0 0 0 0
2 123 A Incoming 0 0 0 0 0 0 0 0 0 0 0 0 0
3 345 B Sell out forecast 2 2 2 2 2 2 2 2 2 0 0 0 0
4 345 B Inventory 0 0 0 0 0 0 0 0 0 0 0 0 0
5 345 B Incoming 0 0 0 0 0 0 0 0 0 0 0 0 0
6 678 C Sell out forecast 0 0 0 0 0 0 0 0 0 0 0 0 0
7 678 C Inventory 0 0 0 0 0 0 0 0 0 0 0 0 0
8 678 C Incoming 0 0 0 0 0 0 0 0 0 0 0 0 0
评论
0赞
Timeless
5/18/2023
注意:我删除了倒数四行,并修复了 .df1
Factor
df2
0赞
Shan Jiang
5/18/2023
#2
我最终得到了这样的 sudo-code:
for index, row in df1.iterrows():
# Update Sell thru forecast
for idx, r in df2.iterrows():
if row["Factor"] == "Sell out Forecast" and row["Item Number"] == r["Item Number"]:
df1.iloc[index,common_week_start:common_week_end+1]=r[common_week]
break
# Update Inventory
# Update Incoming
但一些锻炼前需要做:
- 您需要循环 和 才能获得通用周列表。
df1.column
df2.column
- 设置两个变量,以及
Common_week_start
Common_week_end
- 确保 Factor 名称相同(懒惰的方式,哈哈。另外,可以使用正则表达式)
我一开始失败的原因是我使用了链切片创建数据的副本,每个循环只更改复制值。现在我用了,效果很好。df.iloc[index][common_week]
.iloc[index,column_start:column_end]
评论