title: Python Pandas: how to add a totally new column to a data frame inside of a groupby/transform operation
body: <p>I want to mark some quantiles in my data, and for each row of the DataFrame, I would like the entry in a new column called e.g. "xtile" to hold this value.</p>
  
  <p>For example, suppose I create a data frame like this:</p>
  
  <pre><code>import pandas, numpy as np
  dfrm = pandas.DataFrame({'A':np.random.rand(100), 
                           'B':(50+np.random.randn(100)), 
                           'C':np.random.randint(low=0, high=3, size=(100,))})
  </code></pre>
  
  <p>And let's say I write my own function to compute the quintile of each element in an array. I have my own function for this, but for example just refer to scipy.stats.mstats.mquantile.</p>
  
  <pre><code>import scipy.stats as st
  def mark_quintiles(x, breakpoints):
      # Assume this is filled in, using st.mstats.mquantiles.
      # This returns an array the same shape as x, with an integer for which
      # breakpoint-bucket that entry of x falls into.
  </code></pre>
  
  <p>Now, the real question is how to use <code>transform</code> to add a new column to the data. Something like this:</p>
  
  <pre><code>def transformXtiles(dataFrame, inputColumnName, newColumnName, breaks):
      dataFrame[newColumnName] = mark_quintiles(dataFrame[inputColumnName].values, 
                                                breaks)
      return dataFrame
  </code></pre>
  
  <p>And then:</p>
  
  <pre><code>dfrm.groupby("C").transform(lambda x: transformXtiles(x, "A", "A_xtile", [0.2, 0.4, 0.6, 0.8, 1.0]))
  </code></pre>
  
  <p>The problem is that the above code will not add the new column "A_xtile". It just returns my data frame unchanged. If I first add a column full of dummy values, like NaN, called "A_xtile", then it <em>does</em> successfully over-write this column to include the correct quintile markings.</p>
  
  <p>But it is extremely inconvenient to have to first write in the column for anything like this that I may want to add on the fly.</p>
  
  <p>Note that a simple <code>apply</code> will not work here, since it won't know how to make sense of the possibly differently-sized result arrays for each group.</p>