covariance between two columns in pandas groupby pandas
<p>I am trying to calculate the covariance between two columns by group. I am doing doing the following:</p>

<pre><code>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]})

B = A.groupby('group')

B['value1'].cov(B['value2'])
</code></pre>

<p>Ideally, I would like to get the covariance between X and Y and not the whole variance-covariance matrix, since I only have two columns.</p>

<p>Thank you,</p>

<p>The following code gives you the grouped variance-covariance matrix. You can subset it as you wish to just get the covariances. </p>

<pre><code>import pandas as pd
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]})
print A.groupby('group').cov()
</code></pre>

python pandas