title: Normalize rows of pandas data frame by their sums
body: <p>I have a pandas dataframe containing spectral data and metadata. The columns are labeled with a multiindex so that <code>df['wvl']</code> gives the spectra and <code>df['meta']</code> gives the metadata. Within <code>df['wvl']</code> the column labels are the wavelength values for the spectrometer channels. </p>
  
  <p>What I want to do is normalize each row of <code>df['wvl']</code> by the sum of that row so that adding up the values in the row gives a total of 1.0.</p>
  
  <p>Here's what one row of the dataframe looks like: </p>
  
  <pre><code>df['wvl'].iloc[0]
  246.050003     128.533035
  246.102005     102.756321
  246.156006      99.930775
  ...    
  848.697205     121.313347
  848.896423     127.011662
  849.095703     123.234168
  Name: 0, dtype: float64
  </code></pre>
  
  <p>But when I do something like:</p>
  
  <pre><code>df['wvl'].iloc[0]=df['wvl'].iloc[0]/df['wvl'].iloc[0].sum()
  </code></pre>
  
  <p>Nothing happens! I get the exact same values:</p>
  
  <pre><code>df['wvl'].iloc[0]
  246.050003     128.533035
  246.102005     102.756321
  246.156006      99.930775
  ...    
  848.697205     121.313347
  848.896423     127.011662
  849.095703     123.234168
  Name: 0, dtype: float64
  </code></pre>
  
  <p>If I create a temporary variable to hold the row, I can do the normalization just fine:</p>
  
  <pre><code>temp=df['wvl'].iloc[0]
  
  temp=temp/temp.sum()
  
  temp
  246.050003    0.000027
  246.102005    0.000022
  246.156006    0.000021
                  ...   
  848.697205    0.000026
  848.896423    0.000027
  849.095703    0.000026
  Name: 0, dtype: float64
  </code></pre>
  
  <p>But if I try to replace the dataframe row with the normalized temporary variable, nothing happens:</p>
  
  <pre><code>df['wvl'].iloc[0]=temp
  
  df['wvl'].iloc[0]
  246.050003     128.533035
  246.102005     102.756321
  246.156006      99.930775
                   ...     
  848.697205     121.313347
  848.896423     127.011662
  849.095703     123.234168
  Name: 0, dtype: float64
  </code></pre>
  
  <p>I'm obviously missing something here, but I can't figure out what and it's driving me insane. Help? Thanks in advance!</p>