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

Pandas – Calculate Mean and Variance

For a current project, I would like to calculate both the mean and variance for a group of values.

My existing code calculates the mean through .agg('mean'). I tried to add , 'var' inside the bracket, which however yielded an error:

f"numpy operations are not valid with "
pandas.errors.UnsupportedFunctionCall: numpy operations are not valid with groupby. Use .groupby(…).mean() instead

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

Is there any smart tweak to make the code below work?

newdf = df.groupby(['stock_symbol', 'quarter'])['rating_recommend', 'rating_outlook'].agg('mean')

>Solution :

add ‘var’ for variance in the parenthesis.


newdf = (df.groupby(['stock_symbol', 'quarter'])['rating_recommend', 'rating_outlook']
         .agg('mean', 'var'))
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