Pandas – Calculate Mean and Variance

Question:

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

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

newdf = df.groupby(['stock_symbol', 'quarter'])['rating_recommend', 'rating_outlook'].agg('mean')
Asked By: Malte Susen

||

Answers:

add ‘var’ for variance in the parenthesis.


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