2 回答
TA贡献1786条经验 获得超11个赞
这是使用combine_first之后的一种方法drop_duplicates
df.Inventory=df.Inventory.combine_first(df.drop_duplicates(['Product']).Stock)
df
Out[193]:
Year Week Product Stock Inventory
0 2019 21 A 10 10.0
1 2019 22 A 10 34.0
2 2019 23 A 10 NaN
3 2019 24 A 10 28.0
4 2019 25 C 20 20.0
5 2019 26 C 20 39.0
6 2019 27 C 20 NaN
7 2019 28 B 35 35.0
8 2019 29 B 35 NaN
9 2019 30 B 35 94.0
TA贡献1852条经验 获得超7个赞
鉴于产品组合在一起,您可以使用您的逻辑来更新库存:
first_with_na = (df.Product.ne(df.Product.shift()) # first product row
& df.Inventory.isna() # Inventory is na
)
df.loc[first_with_na, 'Inventory'] = df.Stock
输出:
Year Week Product Stock Inventory
0 2019 21 A 10 10.0
1 2019 22 A 10 34.0
2 2019 23 A 10 NaN
3 2019 24 A 10 28.0
4 2019 25 C 20 20.0
5 2019 26 C 20 39.0
6 2019 27 C 20 NaN
7 2019 28 B 35 35.0
8 2019 29 B 35 NaN
9 2019 30 B 35 94.0
添加回答
举报