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

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

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:

我的目标是获得这个输出

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


目前,我正在使用一个循环内的Python函数来获取这4个变量:
campaign、mean、left、right。以下是该函数:

```python
import numpy as np
def bootstrap_ci(df, variable, classes, repetitions = 1000, alpha = 0.05, random_state=None):
    df = df[[variable, classes]]
    bootstrap_sample_size = len(df)

    mean_diffs = []

    for i in range(repetitions):
        bootstrap_sample = df.sample(n = bootstrap_sample_size, replace = True, random_state = random_state)
        mean_diff = bootstrap_sample.groupby(classes).mean().iloc[1,0] - bootstrap_sample.groupby(classes).mean().iloc[0,0]
        mean_diffs.append(mean_diff)

    left = np.percentile(mean_diffs, alpha/2*100)*(-1)
    right = np.percentile(mean_diffs, 100-alpha/2*100)*(-1)
    mean = -(df.groupby(classes).mean().iloc[1,0] - df.groupby(classes).mean().iloc[0,0])

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

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

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

df = pd.DataFrame([]) --初始化一个新的df

for i in campaigns:     
    print(Lift for: {i}')     
    bootstrap_ci(df[df['campaign_name']==i],'conversion','group')
    df = df.append(i, mean, left, right) --将相关变量附加到df

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

df = pd.DataFrame([]) # 初始化一个新的df

for i in campaigns:     
    print(f'Lift for: {i}')     
    result = bootstrap_ci(df[df['campaign_name']==i],'conversion','group')
    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:

campaign | mean | left | right 
A        | 5%   | 0%   | 10% 
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:

import numpy as np
def bootstrap_ci(df, variable, classes, repetitions = 1000, alpha = 0.05, random_state=None):df = df[[variable, classes]]bootstrap_sample_size = len(df)

mean_diffs = []

for i in range(repetitions):
    bootstrap_sample = df.sample(n = bootstrap_sample_size, replace = True, random_state = random_state)
    mean_diff = bootstrap_sample.groupby(classes).mean().iloc[1,0] - bootstrap_sample.groupby(classes).mean().iloc[0,0]
    mean_diffs.append(mean_diff)

    left = np.percentile(mean_diffs, alpha/2*100)*(-1)
    right = np.percentile(mean_diffs, 100-alpha/2*100)*(-1)
    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).

for i in campaigns:     
    print(Lift for: {i}')     
    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):

df = pd.DataFrame([]) --initiate a new df

for i in campaigns:     
    print(Lift for: {i}')     
    bootstrap_ci(df[df['campaign_name']==i],'conversion','group')
    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:

import numpy as np
import pandas as pd

def bootstrap_ci(df, variable, classes, repetitions=1000, alpha=0.05, random_state=None):
    df = df[[variable, classes]]
    bootstrap_sample_size = len(df)
    mean_diffs = []
    
    for i in range(repetitions):
        bootstrap_sample = df.sample(n=bootstrap_sample_size, replace=True,
                                     random_state=random_state)
        mean_diff = bootstrap_sample.groupby(classes).mean().iloc[1, 0] - bootstrap_sample.groupby(classes).mean().iloc[0, 0]
        mean_diffs.append(mean_diff)
    
    left = np.percentile(mean_diffs, alpha / 2 * 100) * (-1)
    right = np.percentile(mean_diffs, 100 - alpha / 2 * 100) * (-1)
    mean = -(df.groupby(classes).mean().iloc[1, 0] - df.groupby(classes).mean().iloc[0, 0])
   
   # added return statement
   return dict(mean=mean, left=left, right=right)


# initiate a new df
new_df = pd.DataFrame({'mean': [], 'left': [], 'right': []})

for i in campaigns:     
    print('Lift for: {i}')     
    values = bootstrap_ci(df[df['campaign_name'] == i], 'conversion', 'group')
    
    # append the relevant variables to df
    for k, v in values.items():
        new_df.at[i, k] = v 
英文:

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

import numpy as np
import pandas as pd

def bootstrap_ci(df, variable, classes, repetitions=1000, alpha=0.05, random_state=None):
    df = df[[variable, classes]]
    bootstrap_sample_size = len(df)
    mean_diffs = []
    
    for i in range(repetitions):
        bootstrap_sample = df.sample(n=bootstrap_sample_size, replace=True,
                                     random_state=random_state)
        mean_diff = bootstrap_sample.groupby(classes).mean().iloc[1, 0] - bootstrap_sample.groupby(classes).mean().iloc[0, 0]
        mean_diffs.append(mean_diff)
    
    left = np.percentile(mean_diffs, alpha / 2 * 100) * (-1)
    right = np.percentile(mean_diffs, 100 - alpha / 2 * 100) * (-1)
    mean = -(df.groupby(classes).mean().iloc[1, 0] - df.groupby(classes).mean().iloc[0, 0])
   
   # added return statement
   return dict(mean=mean, left=left, right=right)


# initiate a new df
new_df = pd.DataFrame({'mean': [], 'left': [], 'right': []})

for i in campaigns:     
    print('Lift for: {i}')     
    values = bootstrap_ci(df[df['campaign_name'] == i], 'conversion', 'group')
    
    # append the relevant variables to df
    for k, v in values.items():
        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:

确定