在组

时间:2018-04-20 21:13:33

标签: python python-3.x pandas dataframe pandas-groupby

目前正在尝试创建一个函数,我在我的DataFrame中将列分隔为DF_1,并将它们按相同DataFrame中的维度列进行分组。

下面的代码试图通过首先按维度列分组并将lambda函数应用于我尝试划分的每个列来实现此目的,以获得每个指标的平均值,即每次转化的成本,或每次点击费用。

不幸的是,我不确定如何做到这一点。以下代码的错误为TypeError: lambda() takes 2 positional arguments but 3 were given

calc_1 = DF_1[['Conversions_10D', 'Total_Revenue', 'Total_Revenue', 'Clicks', 'Spend']]
calc_2 = DF_1[['Impressions', 'Spend', 'Conversions_10D', 'Impressions', 'Clicks' ]]


def agg_avg(df, group_field, list_a, list_b):
    grouped = df.groupby(group_field, as_index = False).apply(lambda x, y: x/y, list_a, list_b)
    grouped = pd.DataFrame(grouped).reset_index(drop = True)

    return grouped


{'Date': {0: '2018-02-28', 1: '2018-02-28', 2: '2018-02-28', 3: '2018-02-28', 4: '2018-02-28'}, 'Audience_Category': {0: 'Affinity', 1: 'Affinity', 2: 'Affinity', 3: 'Affinity', 4: 'Affinity'}, 
'Demo': {0: 'F25-34', 1: 'F25-34', 2: 'F25-34', 3: 'F25-34', 4: 'F25-34'}, 'Gender': {0: 'Female', 1: 'Female', 2: 'Female', 3: 'Female', 4: 'Female'}, 
'Device': {0: 'Android', 1: 'Android', 2: 'Android', 3: 'Android', 4: 'Android'}, 
'Creative': {0: 'Bubble:15', 1: 'Bubble:30', 2: 'Wide :15', 3: 'Oscar :15', 4: 'Oscar :30'}, 
'Impressions': {0: 3834, 1: 3588, 2: 3831, 3: 3876, 4: 3676}, 
'Clicks': {0: 2.0, 1: 0.0, 2: 4.0, 3: 2.0, 4: 1.0},
'Conversions_10D': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0}, 
'Total_Revenue': {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0}, 'Spend': {0: 28.600707059999991, 1: 25.95319236000001, 2: 28.29383795999998, 3: 29.287063200000013, 4: 26.514734159999968}, 
'Demo_Category': {0: 'Narrow', 1: 'Broad', 2: 'Narrow', 3: 'Broad', 4: 'Narrow'}
'CPM_Efficiency': {0: 'Low CPM', 1: 'Low CPM', 2: 'Low CPM', 3: 'Low CPM', 4: 'Low CPM'}}    

0 个答案:

没有答案