gpt4 book ai didi

python - 计算结转效应

转载 作者:行者123 更新时间:2023-12-01 01:01:07 25 4
gpt4 key购买 nike

我想计算电视广告 GRP 数据的结转效应。我的输入数据如下所示:

      Variable       Date  Causal  Half_Life
0 TV Model 2016-01-10 0 4
1 TV Model 2016-01-17 0 4
2 TV Model 2016-01-24 0 4
3 TV Model 2016-01-31 100 4
4 TV Model 2016-02-07 110 4
5 TV Model 2016-02-14 89 4
6 TV Model 2016-02-21 57 4
7 TV Model 2016-02-28 90 4
8 TV General 2016-01-10 0 4
9 TV General 2016-01-17 0 4
10 TV General 2016-01-24 0 4
11 TV General 2016-01-31 30 4
12 TV General 2016-02-07 32 4
13 TV General 2016-02-14 42 4
14 TV General 2016-02-21 39 4
15 TV General 2016-02-28 55 4

我想根据以下条件计算新列 df['Adstock']:

If first row of the group from Column df.Variable, then df.Adstock = df.Causal If not the first row from the group then, df. Adstock = df.Causal + 0.5**(1/df.Half_life)*df.Adstock from the previous row.

我正在使用以下代码:

import pandas as pd
import numpy as np
import numpy.random as random
import statsmodels.api as sm
import statsmodels.tsa as tsa
import statsmodels.formula.api as smf
import datetime

df = pd.read_excel('RC Data.xlsx')


df['Adstock'] = 0

df['Adstock'] = np.where(df['Variable'] == df['Variable'].shift(1), df['Adstock'].shift(1)*(0.5**(1/df['Half_Life'])) + df['Causal'], df['Causal'])

我得到的输出如下:

      Variable       Date  Causal  Half_Life  Adstock
0 TV Model 2016-01-10 0 4 0.0
1 TV Model 2016-01-17 0 4 0.0
2 TV Model 2016-01-24 0 4 0.0
3 TV Model 2016-01-31 100 4 100.0
4 TV Model 2016-02-07 110 4 110.0
5 TV Model 2016-02-14 89 4 89.0
6 TV Model 2016-02-21 57 4 57.0
7 TV Model 2016-02-28 90 4 90.0
8 TV General 2016-01-10 0 4 0.0
9 TV General 2016-01-17 0 4 0.0
10 TV General 2016-01-24 0 4 0.0
11 TV General 2016-01-31 30 4 30.0
12 TV General 2016-02-07 32 4 32.0
13 TV General 2016-02-14 42 4 42.0
14 TV General 2016-02-21 39 4 39.0
15 TV General 2016-02-28 55 4 55.0

但是所需的输出应该如下所示:

      Variable       Date  Causal  Half_Life     Adstock
0 TV Model 2016-01-10 0 4 0.000000
1 TV Model 2016-01-17 0 4 0.000000
2 TV Model 2016-01-24 0 4 0.000000
3 TV Model 2016-01-31 100 4 100.000000
4 TV Model 2016-02-07 110 4 194.089642
5 TV Model 2016-02-14 89 4 252.209284
6 TV Model 2016-02-21 57 4 269.081883
7 TV Model 2016-02-28 90 4 316.269991
8 TV General 2016-01-10 0 4 0.000000
9 TV General 2016-01-17 0 4 0.000000
10 TV General 2016-01-24 0 4 0.000000
11 TV General 2016-01-31 30 4 30.000000
12 TV General 2016-02-07 32 4 57.226892
13 TV General 2016-02-14 42 4 90.121889
14 TV General 2016-02-21 39 4 114.783173
15 TV General 2016-02-28 55 4 151.520759

请帮忙。

最佳答案

这是我的解决方案,我认为很难将其矢量化

l=[]
for x , y in df.groupby('Variable',sort=False):
#print(y)
l1=[]
for s,t in y.iterrows():
if len(l1)==0:
l1.append(t['Causal'])
else:
l1.append(t['Causal'] + 0.5**(1/t['Half_Life'])*l1[-1])
l.extend(l1)
df['New']=l
df
Out[982]:
Variable Date Causal Half_Life New
0 TVModel 2016-01-10 0 4 0.000000
1 TVModel 2016-01-17 0 4 0.000000
2 TVModel 2016-01-24 0 4 0.000000
3 TVModel 2016-01-31 100 4 100.000000
4 TVModel 2016-02-07 110 4 194.089642
5 TVModel 2016-02-14 89 4 252.209284
6 TVModel 2016-02-21 57 4 269.081883
7 TVModel 2016-02-28 90 4 316.269991
8 TVGeneral 2016-01-10 0 4 0.000000
9 TVGeneral 2016-01-17 0 4 0.000000
10 TVGeneral 2016-01-24 0 4 0.000000
11 TVGeneral 2016-01-31 30 4 30.000000
12 TVGeneral 2016-02-07 32 4 57.226892
13 TVGeneral 2016-02-14 42 4 90.121889
14 TVGeneral 2016-02-21 39 4 114.783173
15 TVGeneral 2016-02-28 55 4 151.520759

关于python - 计算结转效应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55788285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com