bab*_*abz 5 python numpy divide-by-zero weighted-average pandas
创建 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 (屏蔽平均值)的文档,但无法理解如何实现它
这还不够吗?
def wm(x):
try:
return np.average(x, weights=df.loc[x.index, 'WEIGHTS'])
except ZeroDivisionError:
return 0
f = {'DRESS_AMT': 'max',
'FACE_AMT': 'sum',
'Other_AMT': {'weighted_mean' : wm} }
df2=df.groupby(['ID','COL1'], as_index=False).agg(f)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4162 次 |
| 最近记录: |