def corr(df, step=100, mask_diagonal=False):
    n = df.shape[0]

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

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_
c = corr_closure(df)
step = min(step, df.shape[1])
tups = zip(list(range(0, n, step)), list(range(step, n + step, step)))
corr_table = pd.concat([c(*t) for t in tups], axis=1)
if mask_diagonal:
    np.fill_diagonal(corr_table.values, np.nan)
return corr_table
Congruency Table in Pandas (Pearson Correlation between each row for every row pair)