title: Switch columns and rows in dataframe with correct order
body: <p>I have a DataFrame which I wanted to modify but I'm struggling with the results:</p>
  <pre><code>df = pd.DataFrame({&quot;ID&quot;: ['2425128168','4055518718', '4055497871', '4055497871'],
                     &quot;To&quot;: ['A', 'A', 'B', 'C'],
                     &quot;From&quot;: ['D', 'C', 'A', 'A'],
                     &quot;Qty&quot;:[10,20,30,40]})
  df['Qty'] = df['Qty'].astype(int)
  
      ID          To From Qty
  0   2425128168  A   D   10
  1   4055518718  A   C   20
  2   4055497871  B   A   30
  3   4055497871  C   A   40
  </code></pre>
  <p>The idea is to switch column From into different columns from A to F and write a value from Qty column in correct place, for this example the result should be like that:</p>
  <pre><code>    ID          To From Qty A  B C  D  E F
  0   2425128168  A   D   10  0  0 0  10 0 0 
  1   4055518718  A   C   20  0  0 20 0  0 0
  2   4055497871  B   A   30  30 0 0  0  0 0
  3   4055497871  C   A   40  40 0 0  0  0 0
  </code></pre>
  <p>I was trying to do that but I get wrong values when the ID are not unique.
  This is my solution(wrong):</p>
  <pre><code>grouped = df.pivot(index=['ID','To'], columns='From', values='Qty')
  grouped = grouped.fillna(0)
  
  for i in ['A', 'B', 'C', 'D', 'E', 'F']:
      try:
          df[i] = grouped[i].reset_index(drop = True).astype(int)
      except:
          df[i] = 0
  
  df = 
  
        ID    To  From    Qty A   B   C   D   E   F
  0   2425128168  A   D   10  0   0   0   10  0   0
  1   4055518718  A   C   20  30  0   0   0   0   0
  2   4055497871  B   A   30  40  0   0   0   0   0
  3   4055497871  C   A   40  0   0   20  0   0   0
  </code></pre>