title: Fast alternative to run a numpy based function over all the rows in Pandas DataFrame
body: <p>I have a Pandas data frame created the following way:</p>
  
  <pre><code>import pandas as pd
  def create(n):  
      df = pd.DataFrame({ 'gene':["foo",
                              "bar",
                              "qux",
                              "woz"], 
                              'cell1':[433.96,735.62,483.42,10.33], 
                              'cell2':[94.93,2214.38,97.93,1205.30],
                              'cell3':[1500,90,100,80]})
      df = df[["gene","cell1","cell2","cell3"]]
      df = pd.concat([df]*n)
      df = df.reset_index(drop=True)
      return df
  </code></pre>
  
  <p>It looks like this:</p>
  
  <pre><code>In [108]: create(1)
  Out[108]:
    gene   cell1    cell2  cell3
  0  foo  433.96    94.93   1500
  1  bar  735.62  2214.38     90
  2  qux  483.42    97.93    100
  3  woz   10.33  1205.30     80
  </code></pre>
  
  <p>Then I have a function that takes the values of each genes(row)
  to compute a certain score:</p>
  
  <p><a href="https://i.stack.imgur.com/n0NeU.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/n0NeU.jpg" alt="enter image description here"></a></p>
  
  <pre><code>import numpy as np 
  def sparseness(xvec):
      n = len(xvec)
      xvec_sum = np.sum(np.abs(xvec))
      xvecsq_sum = np.sum(np.square(xvec))
  
      denom = np.sqrt(n) - (xvec_sum / np.sqrt(xvecsq_sum))
      enum  = np.sqrt(n) - 1
      sparseness_x = denom/enum
  
      return sparseness_x
  </code></pre>
  
  <p>In reality I need to apply this function on 40K over rows. And currently it runs
  very slow using Pandas 'apply':</p>
  
  <pre><code>In [109]: df = create(10000)
  In [110]: express_df   = df.ix[:,1:]
  In [111]: %timeit express_df.apply(sparseness, axis=1)
  1 loops, best of 3: 8.32 s per loop
  </code></pre>
  
  <p>What's the faster alternative to implement that?</p>