title: How can I apply a user-defined row function onto all rows of a loaded in array?
body: <p>I have written a user defined function that loops through a row of values in order to give the number of zeros between values (distance between values). Those distances are appended into a list and then averaged for a final value of average distance between values. The function works great when I load in a CSV file with just one row of values. However, I would like to be able to apply the function to a file with multiple rows, and then report the output of each row into a dataframe.</p>
  
  <p>This is all being run with python 3.7. I attempted to create a nested loop in order apply the function manually. I have tried the numpy.apply_along_axis function. I have also tried reading the file in as a pandas dataframe, and then using the .apply() function. However, I am a bit unfamiliar with pandas, and when I replaced the numpy indexing in the function with pandas indexing, I began to generate multiple errors.</p>
  
  <p>When I load in a larger CSV file and try to apply it to file[0] for example, the function does not work. It seems to work only when I load in a file with one row of values.</p>
  
  <pre><code>def avg_dist():
       import statistics as st
  
  
      dist = []
      ctr=0
  
  #distances between events
  
      for i in range(len(n)):
          if n[i] &gt; 0 and i &lt; (len(n)-1):
              if n[i+1]==0:
                  i+=1
                  while n[i]==0 and i &lt; (len(n)-1):
                      ctr+=1
                      i+=1
                  dist.append(ctr)
                  ctr=0
              else:
                  i+=1
          else:
              i+=1
  
  
  #Average distance between events
      aved = st.mean(dist)
      return(aved)
  </code></pre>