def sparseness2(xs):
    nr = np.sqrt(xs.shape[1])
a = np.sum(np.abs(xs), axis=1)
b = np.sqrt(np.sum(np.square(xs), axis=1))
sparseness = (nr - a / b) / (nr - 1)
return sparseness
res_arr = sparseness2(express_df.values)
res2 = pd.Series(res_arr, index=express_df.index)
Fast alternative to run a numpy based function over all the rows in Pandas DataFrame