title: Pandas "cut" based on other column
body: <p>I want to use <code>pd.cut</code> (to convert continuous variables into discrete ones) in some variables of my pandas dataframe, but I want that cut to depend on other column. Imagine I want 3 bins.</p>
  
  <p>For example:</p>
  
  <pre><code>+------+------+------+--------+
  | col1 | col2 | col3 | sector |
  +------+------+------+--------+
  | 4.5  | 6    | 7    | a      |
  +------+------+------+--------+
  | 8    | 9    | 17   | a      |
  +------+------+------+--------+
  | 0    | 9    | 8    | b      |
  +------+------+------+--------+
  | 8    | 9    | 0    | b      |
  +------+------+------+--------+
  | 1    | 2    | 3.5  | b      |
  +------+------+------+--------+
  </code></pre>
  
  <p>I want to cut only <code>col1</code> and <code>col2</code> into 3 bins, based on <code>sector</code>, so that for each sector a cut is performed. This is very usefull to compare variables from different sources.</p>
  
  <p>The result would be (it's made up, don't expect it to be 100% accurate):</p>
  
  <pre><code>+----------+----------+------+--------+
  | col1_cut | col2_cut | col3 | sector |
  +----------+----------+------+--------+
  | 2        | 2        | 7    | a      |
  +----------+----------+------+--------+
  | 3        | 3        | 17   | a      |
  +----------+----------+------+--------+
  | 1        | 3        | 8    | b      |
  +----------+----------+------+--------+
  | 3        | 3        | 0    | b      |
  +----------+----------+------+--------+
  | 1        | 1        | 3.5  | b      |
  +----------+----------+------+--------+
  </code></pre>
  
  <blockquote>
    <p>PS: I made this Q&amp;A because I had this problem and couldn't find a
    solution myself. Feel free to answer your own solution or improve
    mine, I appreciate the feedback.</p>
  </blockquote>