4

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

rafaelc
  • 57,686
  • 15
  • 58
  • 82
babz
  • 469
  • 6
  • 16

2 Answers2

5

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)
rafaelc
  • 57,686
  • 15
  • 58
  • 82
1

you can use np.ma.average(x, weights=df.loc[x.index, 'WEIGHTS'])