title: How to vectorize python for loop that modifies each element of a dataframe?
body: <p>I have a Python script, using pandas dataframes, that fills a dataframe by converting the elements of another dataframe. I could do it with a simple for loop or itertuples, but I wanted to see if it was possible to vectorize it for maximum speed (my dataframe is very large, ~60000x12000).</p>
  
  <p>Here is an example of what I'm trying to do:</p>
  
  <pre><code>    #Sample data
      sample_list=[1,2,5]
  </code></pre>
  
  <p>I have a list of values like the one above. Each element in my new matrix is the sum of certain two elements from this list divided by a constant number n.</p>
  
  <pre><code>new_matrix[row,col]=(sample_list[row]+sample_list[col])/n
  </code></pre>
  
  <p>So the expected output for n=2 would be:</p>
  
  <pre><code>1   1.5 3
  1.5 2   3.5
  3   3.5 5
  </code></pre>
  
  <p>Right now I execute this with a for loop, iterating across each element of an empty matrix and setting them to the value calculated by the formula. Is there any way this operation could be vectorized (i.e. something like <code>new_matrix=2*old_matrix</code> rather than 
  <code>for row, col in range(): new_matrix[row,col]=2*old_matrix[row,col]?</code></p>