问题
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