pythonpandasaggregate-functionsvalueerror

ValueError: Must produce aggregated value in pandas


I'm getting a ValueError when applying np.sum that I need to product aggregated value. Any word of advise to fix this issue? Btw - This logic used to run for me before.

df1 = 
self_id  |  id  |  rating  |          comment            |
1          820     (blank)            (blank)   
2          823     strong      good performance
3          826     weak               (blank)

#Pivoting the NaNs/unique values
ndf1 = pd.pivot_table(data=df1, index=['self_id'], 
                     aggfunc={'id':np.unique, 
                              'Ratings':np.sum, 
                              'Comments':np.sum})
ValueError: Must produce aggregated value

Thank you in advance!


Solution

  • np.sum return a unique element per group (its sum), while np.unique returns an array. There is an internal check to return this error when an non object array is returned.

    You could convert to list to avoid this issue:

    ndf1 = pd.pivot_table(data=df1, index=['self_id'], 
                         aggfunc={'id':lambda x: np.unique(x).tolist(), 
                                  'rating':np.sum, 
                                  'comment':np.sum})
    

    Output:

                      comment     id  rating
    self_id                                 
    1                       0  [820]       0
    2        good performance  [823]  strong
    3                       0  [826]    weak