def corr_closure(df):
    d = df.values
sums = d.sum(0, keepdims=True)
stds = d.std(0, keepdims=True)
n = d.shape[0]

def corr(k=0, l=10):
    d2 = d.T.dot(d[:, k:l])
sums2 = sums.T.dot(sums[:, k:l])
stds2 = stds.T.dot(stds[:, k:l])
return pd.DataFrame((d2 - sums2 / n) / stds2 / n, df.columns, df.columns[k:l])
return corr
Congruency Table in Pandas (Pearson Correlation between each row for every row pair)