Pandas/numpy weighted average ZeroDivisionError
问题 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