Malte Susen
Malte Susen

Reputation: 845

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

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

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

Upvotes: 0

Views: 577

Answers (1)

Naveed
Naveed

Reputation: 11650

add 'var' for variance in the parenthesis.


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

Upvotes: 2

Related Questions