擅长:python、mysql、java
<pre><code>import pandas as pd
import numpy as np
A = pd.DataFrame({'one':['a','a','a','b','b','b'], 'two':[1,2,3,3,2,1]})
B = pd.DataFrame({'one':['a','a','a','b','b','b'], 'two':[4,3,2,2,3,4]})
A = A.set_index('one').sort_index()
B = B.set_index('one').sort_index()
# as they must have the same number of obs on a or b in both dfs, do horizontal concat
df = pd.concat([A, B], keys=['A', 'B'], axis=1)
def cal_corr(group):
return pd.Series({'corr': group.A.corrwith(group.B).values[0]})
df.groupby(level='one').apply(cal_corr)
Out[211]:
corr
one
a -1
b -1
</code></pre>