title: pandas normalization of groupby.sum
body: <p>I have a pandas dataframe that looks like this:</p>
  
  <pre><code>      **I     SI     weights**
          1     3      0.3  
          2     4      0.2
          1     3      0.5
          1     5      0.5
  </code></pre>
  
  <p>I need to do this: given a value of I, consider each value of SI and add the total weight. At the end, I should have, for each realization, something like that:</p>
  
  <pre><code>             I = 1     SI = 3      weight = 0.8
                         SI = 5      weight = 0.5
  
               I = 2     SI = 4      weight = 0.2
  </code></pre>
  
  <p>This is easily achievable by calling groupby and sum:   </p>
  
  <pre><code>       name = ['I', 'SI','weight']
         Location = 'Simulationsdata/prova.csv'
         df = pd.read_csv(Location, names = name,sep='\t',encoding='latin1') 
  
         results = df.groupby(['I', 'real', 'SI']).weight.sum()
  </code></pre>
  
  <p>Now I want the weights to be normalized to one, so that it should be something like that:</p>
  
  <pre><code>             I = 1     SI = 3      weight = 0.615
                         SI = 5      weight = 0.385
  
               I = 2     SI = 4      weight = 1
  </code></pre>
  
  <p>I tried  this:</p>
  
  <pre><code>        for idx2, j in enumerate(results.index.get_level_values(1).unique()):
              norm = [float(i)/sum(results.loc[j]) for i in results.loc[j]]
  </code></pre>
  
  <p>but when I try to plot, for each I, the distribution of the SI, I find that also the SI are normalized, and I do not want this to happen.</p>
  
  <p>P.s. this question is related to <a href="https://stackoverflow.com/questions/52235697/pandas-groupby-plot-values/52235989?noredirect=1#comment91425482_52235989">this one</a> but, since it involves another aspect of the problem, I tought that it would be better to ask it separately</p>