创建 lambda 函数来计算加权平均值并将其发送到字典。
wm = lambda x: np.average(x, weights=df.loc[x.index, 'WEIGHTS'])
# Define a dictionary with the functions to apply for a given column:
f = {'DRESS_AMT': 'max',
'FACE_AMT': 'sum',
'Other_AMT': {'weighted_mean' : wm}}
# Groupby and aggregate with dictionary:
df2=df.groupby(['ID','COL1'], as_index=False).agg(f)
Run Code Online (Sandbox Code Playgroud)
此代码有效,但如果权重总计为 0 ,则加权平均 lambda 函数会失败ZeroDivisionError。在这些情况下,我希望输出“Other_AMT”仅为 0。
我阅读了有关使用 np.ma.average (屏蔽平均值)的文档,但无法理解如何实现它
数据每个ID多行
ID Value1 Value2
1 1 0
1 0 1
1 3 1
Run Code Online (Sandbox Code Playgroud)
期望的输出
对于每个ID,(SUM(Value1))*(Value2).
在这种情况下,对于ID1,它将是4*0 = 0.
我希望将结果放回原始表中,如下所示
ID Value1 Value2 Calculated_Value
1 1 0 0
1 0 1 4
1 3 1 4
Run Code Online (Sandbox Code Playgroud)
我试过这个..但是我在输出表中得到了NaN ..
df['Calculated_Value'] = df['ID'].map(df.groupby('ID')['Value1'].sum()*['Value2'])
Run Code Online (Sandbox Code Playgroud) df有
id measure t1 t2 t3
1 savings 1 2 5
1 income 10 15 14
1 misc 5 5 5
2 savings 3 6 12
2 income 4 20 80
2 misc 1 1 1
Run Code Online (Sandbox Code Playgroud)
df希望-为每个id的度量添加新行,称为支出,方法是针对每个id的每个期间t1,t2,t3减去measure = income-measure = savings
id measure t1 t2 t3
1 savings 1 2 5
1 income 10 15 14
1 misc 5 5 5
1 spend 9 13 9
2 savings 3 6 12
2 income 4 20 80
2 misc 1 1 …Run Code Online (Sandbox Code Playgroud)