def sparseness_vectorized(A):
    nsqrt = np.sqrt(A.shape[1])
B = np.einsum('ij->i', np.abs(A)) / np.sqrt(np.einsum('ij,ij->i', A, A))
denom = nsqrt - B
enum = nsqrt - 1
return denom / enum
Fast alternative to run a numpy based function over all the rows in Pandas DataFrame