title: Python: reallocate weights with a cap
body: <p>How can I reallocate the weights of a normalized dataframe and impose a cap.</p>
  <p>For example, if I have the following row of data:</p>
  <pre><code>0.1 0.3 0.5 0.1
  </code></pre>
  <p>and I do not want any weight greater than 0.4. How can I clip the 0.5 weight and redistribute the weight such that I maximize each entry. So I would get:</p>
  <pre><code>0.1 0.4 0.4 0.1
  </code></pre>
  <p>So, the 0.5 was clipped to 0.4, and the remaining 0.1 was added to 0.3 to get 0.4.
  Note how in both cases, the entries sum to 1 (normalized).</p>
  <p>Can this be done pythonically? i.e. Without having loops.</p>
  <p>Ideally I want this to applied to a dataframe such as this:</p>
  <pre><code>df = pd.DataFrame({'a': [5003, 54.06, 53.654, 55.2], 'b': [np.nan, 54.1121, 53.98, 55.12], 'c': [np.nan, 2, 53.322, 54.99],
                 'd': [np.nan, 53.1, 53.212, 55.002], 'e': [np.nan, 53, 53.2, 55.021], 'f': [np.nan, 53.11, 53.120, 55.3]})
  N = 5 # 1/np.sqrt(N) = 0.447214
  df = df.div(df.sum(axis=1), axis=0)
  df:
          a           b            c          d           e           f
      0   1.000000    NaN          NaN        NaN         NaN         NaN
      1   0.200681    0.200875    0.007424    0.197118    0.196747    0.197155
      2   0.167413    0.168431    0.166378    0.166034    0.165997    0.165747
      3   0.166952    0.166711    0.166317    0.166354    0.166411    0.167255
  </code></pre>
  <p>Thank you.</p>