Weighted Average Multiple Columns: TypeError: unhashable type: 'list'

Question:

I have a table that looks like this:

charge         (0.0, 1.0)   (0.0, 2.0)       (0.0, 3.0)
84          
116                 1            10                147
226                 9            842               342
343                 2            278
503                 10
939         

I’m attempting to calculate the weighted average of each column and the post below as very helpful. Pandas Group Weighted Average of Multiple Columns. So for example, column (0.0, 1.0), the weighted average would be 357.54.

However, I am doing something wrong where my code produces the following error. I’ve done some research on this error but am still unsure how to fix it Understanding slicing.

**CODE**: 
    def weighted(x, cols, w="weights"):
                 return pd.Series(np.average(x[cols], weights=x[w], axis=0), cols)
    
    FIN_A_PIV_Table.apply(weighted, ['(0.0, 1.0)','(0.0, 2.0)','(0.0, 3.0)']) 

**ERROR**:
        File Q:VDIPython SourceProvider_Coder_Updated_v_Spyder.py:372 in <module>
            FIN_A_PIV_Table.apply(weighted, ['(0.0, 1.0)','(0.0, 2.0)','(0.0, 3.0)'])
        
          File C:Researchlibsite-packagespandascoreframe.py:8839 in apply
            op = frame_apply(
        
          File C:Researchlibsite-packagespandascoreapply.py:88 in frame_apply
            axis = obj._get_axis_number(axis)
    
      File C:Researchlibsite-packagespandascoregeneric.py:550 in _get_axis_number
        return cls._AXIS_TO_AXIS_NUMBER[axis]
    
    TypeError: unhashable type: 'list'
Asked By: Raven

||

Answers:

The weighted average is the sum of the values multiplied by the weights, divided by the sum of the weights.

You can calculate your weighted sum this way:

# get all columns but "charge"
df2 = df.drop(columns='charge')

# multiply the charge by the weights and sum
# divide by the sum of the weights
out = df2.mul(df['charge'], axis=0).sum().div(df2.sum())

Output:

(0.0, 1.0)    357.545455
(0.0, 2.0)    253.810619
(0.0, 3.0)    192.932515
dtype: float64
Answered By: mozway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.