Pandas/numpy weighted average ZeroDivisionError

只愿长相守 提交于 2021-01-03 07:07:27

问题


Creating a lambda function to calculate weighted average and sending that to a dictionary.

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)

This code works but the weighted average lambda function fails if the weights add up to 0 ZeroDivisionError. In these case(s) I want the output 'Other_AMT' to just be 0.

I read a document on using np.ma.average (masked average) but could not understand how to implement it


回答1:


Shouldn't this be enough?

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)


来源:https://stackoverflow.com/questions/49864679/pandas-numpy-weighted-average-zerodivisionerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!