title: pandas cut - different bins for different labels
body: <p>I have a data frame with 2 different labels, A and B, and an associated numeric value.
  I want to add a column giving the label of a custom bin that the numeric value falls in to, which can be achieved with pd.cut() as follows:</p>
  <pre><code>df = pd.DataFrame({&quot;label&quot;: ['A','A','A','A','A','A','B','B','B','B'],
                     &quot;num&quot;:   [ 1 , 2 , 4 , 5 , 10, 11, 1 , 3 , 4 , 5 ]})
  
  df['Bin'] = pd.cut(df[&quot;num&quot;],
                     [0, 4.5, 7.5, np.inf],
                     labels=['0-4', '5-8', '&gt;8'],
                     include_lowest=True)
  </code></pre>
  <p>giving:</p>
  <pre><code>  label  num  Bin
  0     A    1  0-4
  1     A    2  0-4
  2     A    4  0-4
  3     A    5  5-8
  4     A   10   &gt;8
  5     A   11   &gt;8
  6     B    1  0-4
  7     B    3  0-4
  8     B    4  0-4
  9     B    5  5-8
  </code></pre>
  <p>However, this works well for A, but the values of B are such that the most values fall into the bottom bin, so I'd like to increase the resolution with different bins for A and B to produce the following:</p>
  <pre><code>  label  num  Bin
  0     A    1  0-4
  1     A    2  0-4
  2     A    4  0-4
  3     A    5  5-8
  4     A   10   &gt;8
  5     A   11   &gt;8
  6     B    1  0-2
  7     B    3  2-4
  8     B    4  2-4
  9     B    5   &gt;4
  </code></pre>
  <p>It feels like this should be possible using a conditional such as <code>df.where()</code>, or maybe a <code>groupby</code> with a <code>transform()</code> or <code>apply()</code>, or list comprehension with <code>if</code>, but I have been reading stackoverflow and messing around all day and not managed to achieve anything.</p>
  <p>I guess I could separate into individual data frames based on <code>label</code>, perform a custom <code>cut</code> to this sub-dataframue, and then concatenate the results back together, but this doesn't feel very pythonic, or lend itself to generalisable code.</p>
  <p>PS - This is a minimal example, my real data frame has more <code>label</code> values, and I want to keep it as a single data frame with differing bins for further processing in my code, hence not separating into two separate data frames based on <code>label</code>.</p>