如何将来自在Python循环中传递的函数中的变量附加到数据框?

huangapple go评论98阅读模式
英文:

How do I append variables to a dataframe coming from a function passed in a Python loop?

问题

Here is the translated code section you provided:

  1. 我的目标是获得这个输出

campaign | mean | left | right
A | 5% | 0% | 10%
B | 8% | 2% | 6% etc.

  1. 目前,我正在使用一个循环内的Python函数来获取这4个变量:
  2. campaignmeanleftright。以下是该函数:
  3. ```python
  4. import numpy as np
  5. def bootstrap_ci(df, variable, classes, repetitions = 1000, alpha = 0.05, random_state=None):
  6. df = df[[variable, classes]]
  7. bootstrap_sample_size = len(df)
  8. mean_diffs = []
  9. for i in range(repetitions):
  10. bootstrap_sample = df.sample(n = bootstrap_sample_size, replace = True, random_state = random_state)
  11. mean_diff = bootstrap_sample.groupby(classes).mean().iloc[1,0] - bootstrap_sample.groupby(classes).mean().iloc[0,0]
  12. mean_diffs.append(mean_diff)
  13. left = np.percentile(mean_diffs, alpha/2*100)*(-1)
  14. right = np.percentile(mean_diffs, 100-alpha/2*100)*(-1)
  15. mean = -(df.groupby(classes).mean().iloc[1,0] - df.groupby(classes).mean().iloc[0,0])

接下来,我正在创建一个循环来运行所有我的广告系列的自举函数(这个工作)。

  1. for i in campaigns:
  2. print(Lift for: {i}')
  3. bootstrap_ci(df[df['campaign_name']==i],'conversion','group')

然后,我尝试将以下代码添加到循环函数中,以将left、right、mean这些变量附加到df(这个不起作用):

  1. df = pd.DataFrame([]) --初始化一个新的df
  2. for i in campaigns:
  3. print(Lift for: {i}')
  4. bootstrap_ci(df[df['campaign_name']==i],'conversion','group')
  5. df = df.append(i, mean, left, right) --将相关变量附加到df

请注意,你在附加变量时需要将它们包装在字典或Series中,以确保正确附加到DataFrame中。你可以使用类似以下的代码:

  1. df = pd.DataFrame([]) # 初始化一个新的df
  2. for i in campaigns:
  3. print(f'Lift for: {i}')
  4. result = bootstrap_ci(df[df['campaign_name']==i],'conversion','group')
  5. df = df.append({'campaign': i, 'mean': result['mean'], 'left': result['left'], 'right': result['right']}, ignore_index=True)

这样,你将能够将每个广告系列的结果添加到DataFrame中。

英文:

My goal is to have this output:

  1. campaign | mean | left | right
  2. A | 5% | 0% | 10%
  3. B | 8% | 2% | 6% etc.

Right now, I am using a python function within a loop to get the 4 variables:
campaign, mean, left, right. Here is the function:

  1. import numpy as np
  2. def bootstrap_ci(df, variable, classes, repetitions = 1000, alpha = 0.05, random_state=None):df = df[[variable, classes]]bootstrap_sample_size = len(df)
  3. mean_diffs = []
  4. for i in range(repetitions):
  5. bootstrap_sample = df.sample(n = bootstrap_sample_size, replace = True, random_state = random_state)
  6. mean_diff = bootstrap_sample.groupby(classes).mean().iloc[1,0] - bootstrap_sample.groupby(classes).mean().iloc[0,0]
  7. mean_diffs.append(mean_diff)
  8. left = np.percentile(mean_diffs, alpha/2*100)*(-1)
  9. right = np.percentile(mean_diffs, 100-alpha/2*100)*(-1)
  10. mean = -(df.groupby(classes).mean().iloc[1,0] - df.groupby(classes).mean().iloc[0,0])

Next, I am creating a loop to run the bootstrap function for all my campaigns (this works).

  1. for i in campaigns:
  2. print(Lift for: {i}')
  3. bootstrap_ci(df[df['campaign_name']==i],'conversion','group')

Then, I tried adding this code to the loop function to append the variables left, right, mean (this does not work):

  1. df = pd.DataFrame([]) --initiate a new df
  2. for i in campaigns:
  3. print(Lift for: {i}')
  4. bootstrap_ci(df[df['campaign_name']==i],'conversion','group')
  5. df = df.append(i, mean, left, right) --append the relevant variables to df

Please advise as the df returns nothing...

答案1

得分: 0

Append new items with df.at[index, column] = value, as follows:

  1. import numpy as np
  2. import pandas as pd
  3. def bootstrap_ci(df, variable, classes, repetitions=1000, alpha=0.05, random_state=None):
  4. df = df[[variable, classes]]
  5. bootstrap_sample_size = len(df)
  6. mean_diffs = []
  7. for i in range(repetitions):
  8. bootstrap_sample = df.sample(n=bootstrap_sample_size, replace=True,
  9. random_state=random_state)
  10. mean_diff = bootstrap_sample.groupby(classes).mean().iloc[1, 0] - bootstrap_sample.groupby(classes).mean().iloc[0, 0]
  11. mean_diffs.append(mean_diff)
  12. left = np.percentile(mean_diffs, alpha / 2 * 100) * (-1)
  13. right = np.percentile(mean_diffs, 100 - alpha / 2 * 100) * (-1)
  14. mean = -(df.groupby(classes).mean().iloc[1, 0] - df.groupby(classes).mean().iloc[0, 0])
  15. # added return statement
  16. return dict(mean=mean, left=left, right=right)
  17. # initiate a new df
  18. new_df = pd.DataFrame({'mean': [], 'left': [], 'right': []})
  19. for i in campaigns:
  20. print('Lift for: {i}')
  21. values = bootstrap_ci(df[df['campaign_name'] == i], 'conversion', 'group')
  22. # append the relevant variables to df
  23. for k, v in values.items():
  24. new_df.at[i, k] = v
英文:

Append new items with df.at[index, column] = value, as follows:

  1. import numpy as np
  2. import pandas as pd
  3. def bootstrap_ci(df, variable, classes, repetitions=1000, alpha=0.05, random_state=None):
  4. df = df[[variable, classes]]
  5. bootstrap_sample_size = len(df)
  6. mean_diffs = []
  7. for i in range(repetitions):
  8. bootstrap_sample = df.sample(n=bootstrap_sample_size, replace=True,
  9. random_state=random_state)
  10. mean_diff = bootstrap_sample.groupby(classes).mean().iloc[1, 0] - bootstrap_sample.groupby(classes).mean().iloc[0, 0]
  11. mean_diffs.append(mean_diff)
  12. left = np.percentile(mean_diffs, alpha / 2 * 100) * (-1)
  13. right = np.percentile(mean_diffs, 100 - alpha / 2 * 100) * (-1)
  14. mean = -(df.groupby(classes).mean().iloc[1, 0] - df.groupby(classes).mean().iloc[0, 0])
  15. # added return statement
  16. return dict(mean=mean, left=left, right=right)
  17. # initiate a new df
  18. new_df = pd.DataFrame({'mean': [], 'left': [], 'right': []})
  19. for i in campaigns:
  20. print('Lift for: {i}')
  21. values = bootstrap_ci(df[df['campaign_name'] == i], 'conversion', 'group')
  22. # append the relevant variables to df
  23. for k, v in values.items():
  24. new_df.at[i, k] = v

huangapple
  • 本文由 发表于 2023年4月20日 02:00:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057568.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定