我有一个有关促销的产品编号数据框。列包括产品编号、开始日期、结束日期、促销类型和促销说明。日期最多可跨越4个月。我需要添加行以说明开始日期和结束日期之间的月份。
以下是当前数据的示例:
import pandas as pd
sales_dict = {}
sales_dict['item'] = ['100179K', '100086K']
sales_dict['start_date'] = [201703, 201801]
sales_dict['end_date'] = [201707, 201802]
sales_dict['promotin_type'] = [1,0]
sales_dict['promotion_desc'] = [0,1]
df = pd.DataFrame.from_dict(sales_dict)
我试图创建一个年_月格式的数据框架,从时间框架的开始到结束,然后连接两个数据集。
我还查看了Creating a single column of dates from a column of start dates and a column of end dates - python,但现在不确定是否正确填充了所有其他列。
这就是我想要的。
sales_dict = {}
sales_dict['item'] = ['100179K','100179K','100179K','100179K','100179K','100086K','100086K']
sales_dict['start_date'] = [201703, 201704, 201705, 201706, 201707, 201801, 201802]
sales_dict['promotin_type'] = [1,1,1,1,1, 0,0]
sales_dict['promotion_desc'] = [0,0,0,0,0,1,1]
df = pd.DataFrame.from_dict(sales_dict)
1条答案
按热度按时间piok6c0g1#
我想出来了,虽然可能不太优雅: