我正在制作一个看起来像这样的数据帧:
Product_id month sales
1 01-2018 25
1 02-2018 34
1 03-2018 29
1 04-2018 45
2 02-2018 3
2 04-2018 2
销售额在2018年1月至2020年9月之间。 但我的产品2在2018年3月尚未售出。因此,我尝试添加一行,其中Product_id为2,month=03-2018,sales=0。我不想在2018年1月添加一行,因为如果第一次销售是在2018年2月,这意味着该产品在2018年1月无法上市
我得到了第一个销售月份的产品代码:
df.groupby('Product_id').month.min().reset_index()
现在,如果数据不存在,我将尝试为每个月的每个产品添加行。 我还没有什么好用的。 任何想法都会受到欢迎
提前谢谢(对不起,我的英语很差!!)
使用:
我想不出任何直接的解决办法。您可以使用以下代码段
结果将得到总共23行。产品1为12,产品2为11(因为我们忽略了第一个月)
相关问题 更多 >
编程相关推荐