title: Sort pandas df into individual columns
body: <p>I am trying to <code>sort</code> a <code>pandas df</code> into individual <code>columns</code> based on when values in <code>columns</code> change. For the <code>df</code> below I can sort the df into separate <code>columns</code> when a values changes in <code>Col B</code>. But I'm trying to add <code>Col C</code>so it's when values change in both <code>Col B</code> and <code>Col C</code>.</p>
  
  <pre><code>import pandas as pd
  
  df = pd.DataFrame({
          'A' : [10,20,30,40,40,30,20,10,5,10,15,20,20,15,10,5],
          'B' : ['X','X','X','X','Y','Y','Y','Y','X','X','X','X','Y','Y','Y','Y'],
          'C' : ['W','W','Z','Z','Z','Z','W','W','W','W','Z','Z','Z','Z','W','W'],                                         
          })
  
  d = df['B'].ne(df['B'].shift()).cumsum()
  df['C'] =  d.groupby(df['B']).transform(lambda x: pd.factorize(x)[0]).add(1).astype(str)
  df['D'] = df.groupby(['B','C']).cumcount()
  df = df.set_index(['D','C','B'])['A'].unstack([2,1])
  df.columns = df.columns.map(''.join)
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>   X1  Y1  X2  Y2
  D                
  0  10  40   5  20
  1  20  30  10  15
  2  30  20  15  10
  3  40  10  20   5
  </code></pre>
  
  <p>As you can see, this creates a new <code>column</code> every time there's a new value in <code>Col B</code>. But I'm trying to incorporate <code>Col C</code> as well. So it should be every time there's a change in both <code>Col B</code> and <code>Col C</code>.</p>
  
  <p>Intended output:</p>
  
  <pre><code>   XW1  XZ1  YZ1  YW1  XW2  XZ2  YZ2  YW2
  0   10   30   40   20    5   15   20   10
  1   20   40   30   10   10   20   15    5
  </code></pre>