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')
>Solution :
add ‘var’ for variance in the parenthesis.
newdf = (df.groupby(['stock_symbol', 'quarter'])['rating_recommend', 'rating_outlook']
.agg('mean', 'var'))