title: Pandas add column from cuts to DataFrame
body: <p>I need to record <code>cut</code>s (sub-bins) on <code>cut</code>s of a <code>DataFrame</code>.</p>
  
  <p>This is very simple if the sub-bin bounds are the same for every <code>cut</code>.  E.g.,</p>
  
  <pre><code>df = pd.DataFrame({'A':np.random.random(100), 'B':np.random.random(100)})
  # Primary bins: quintiles on column A
  df['P'] = pd.qcut(df['A'], 5, labels=range(1,6)).astype(int)
  # Secondary bins: quartiles on column B
  df['Q'] = df.groupby(['P'])['B'].transform(lambda x: pd.qcut(x, 4, labels=range(1,5)))
  </code></pre>
  
  <p>However, I can't figure out how to use a transform function, or even how to get the second <code>cut</code> values back into the DataFrame, when the <code>cut</code> bounds are different for each primary <code>cut</code>.  E.g.,</p>
  
  <pre><code>subBinBounds = [[0, .1, .5, 1],[0, .3, .6, 1],[0, .2, .7, 1],[0, .4, .6, 1][0, .2, .5, 1]]
  for i in range(5):
      cut = df[df['P'] == i+1]  # P is in {1, 5}
      subbin = pd.cut(cut['B'], subBinBounds[i], labels=range(1,4))
      cut['Q'] = cut.assign(Q=subbin.values)
      # But how do we get 'Q' back into df?
  </code></pre>