My DataFrame:
A = pd.DataFrame({'group':['A','A','A','A','B','B','B'],
'value1':[1,2,3,4,5,6,7],
'value2':[8,5,4,3,7,8,8]})
I'd like to have a new column, covariance, that show the covariance between [value1,value2] on the group-level, for each observation.
Therefore I tried:
A['covariance'] = A.groupby('group')[['value1','value2']].transform(lambda x: x['value1'].cov(x['value2']),axis=1)
But this results in error:
TypeError: Transform function invalid for data types
What am I missing?