有两个数据框:
id Country Channel Week Value
0 ES Train 2018-48 1000
1 ES Train 2018-49 1000
2 ES Train 2018-50 1000
3 ES Train 2018-51 1000
4 ES Train 2018-52 1000
和
Week Product1 Product2 Product3
2018-48 25% 50% 25%
2018-49 25% 50% 25%
2018-50 25% 50% 25%
2018-51 25% 50% 25%
2018-52 25% 50% 25%
如何创建两者的组合,我将每个产品添加到第一帧的每一行并使用乘以产品本身的值?
例如在前两周,这将导致:
id Country Channel Week Value Product
0 ES Train 2018-48 250 Product1
1 ES Train 2018-48 500 Product2
2 ES Train 2018-48 250 Product3
3 ES Train 2018-49 250 Product1
4 ES Train 2018-49 500 Product2
5 ES Train 2018-49 250 Product3
6 ...
请您参考如下方法:
假设您从数据帧 df1 和 df2 开始,首先熔化 df2(从宽到长),将您的百分比转换为数字,然后与 df1 合并:
df2 = pd.melt(df2, id_vars='Week', value_vars=['Product1', 'Product2', 'Product3'])
df2['value'] = pd.to_numeric(df2['value'].str[:-1])
res = df1.merge(df2)\
.eval('Value = Value * value / 100')\
.drop('value', 1)
print(res)
id Country Channel Week Value variable
0 0 ES Train 2018-48 250.0 Product1
1 0 ES Train 2018-48 500.0 Product2
2 0 ES Train 2018-48 250.0 Product3
3 1 ES Train 2018-49 250.0 Product1
4 1 ES Train 2018-49 500.0 Product2
5 1 ES Train 2018-49 250.0 Product3
6 2 ES Train 2018-50 250.0 Product1
7 2 ES Train 2018-50 500.0 Product2
8 2 ES Train 2018-50 250.0 Product3
9 3 ES Train 2018-51 250.0 Product1
10 3 ES Train 2018-51 500.0 Product2
11 3 ES Train 2018-51 250.0 Product3
12 4 ES Train 2018-52 250.0 Product1
13 4 ES Train 2018-52 500.0 Product2
14 4 ES Train 2018-52 250.0 Product3
