Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading