title: Is there any way to speed up dependent iterations in a loop (numpy or pandas) like vectorize?
body: <p>I have a model with pandas dataframe or numpy arrays as inputs. The model iterates over the rows in a loop with the current row calculation dependent on the prior step. Is there any way to incorporate numpy vectorize in this? or any other way to make the code run faster. I have very big inputs and any improvement in speed saves a lot of time. A sample code is below for reference. NUmpy inputs improve speed over the pandas dataframe inputs. Any suggestion is highly appreciated.</p>
  <pre><code>    import pandas as pd
      import numpy as np
      inp1 = pd.DataFrame(np.random.rand(5,5))
      inp2 = pd.DataFrame(np.random.rand(5,5))
  
      outp1 = pd.DataFrame(np.zeros(inp1.shape, dtype = float),
                   index = inp1.index, columns = inp1.columns)
  
      def sample_code_pandas(params):
          aa, bb, lmt = params
          outp1[inp2 &lt; lmt] = inp1[inp2 &lt; lmt]
          out_pr = outp1.iloc[0,:]    
  
          for i in range(1, len(outp1)):
              rates = ((inp1.iloc[i,:] - aa) / (aa - bb))
              outp1.iloc[i,:] = inp1.iloc[i,:] * rates - out_pr
              out_pr = outp1.iloc[i,:]
          return outp1
      %timeit sample_code_pandas((-0.2, -0.5, 0 ))
  
      #******************************************************
  
      inp1 = np.random.rand(5,5)
      inp2 = np.random.rand(5,5)
      outp1 = np.zeros(inp1.shape, dtype = float)
      
      def sample_code_numpy(params):
          aa, bb, lmt = params
          outp1[inp2 &lt; lmt] = inp1[inp2 &lt; lmt]
          out_pr = outp1[0]
      
          for i in range(1, len(outp1)):
              rates = ((inp1[i] - aa) / (aa - bb))
              outp1[i] = inp1[i] * rates - out_pr
              out_pr = outp1[i]
          return outp1
      %timeit sample_code_numpy((-0.2, -0.5, 0 ))
  </code></pre>