cs = np.sum(a[:, :n - i] * r[:, :n - i], axis=0) / (np.sqrt(np.sum(a[:, :n -
    i] * a[:, :n - i], axis=0)) * np.sqrt(np.sum(r[:, :n - i] * r[:, :n - i
    ], axis=0)))
out[i:n * n - i * n:n + 1] = cs
return out.reshape((n, n))
efficient way to calculate distance between combinations of pandas frame columns