title: Convert data in "grouped" columns from long to wide / square format pd.DataFrame
body: <p>Not quite sure how to describe what I'm looking for, so hopefully an example will help.</p>
  <p><a href="https://i.stack.imgur.com/DSemO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DSemO.png" alt="enter image description here" /></a></p>
  <ul>
  <li>Each row contains up to n sets of observations (here, n=4)</li>
  <li>The observations are grouped by (id, x, y)</li>
  <li>The id represents some sort of unique location and the x, y are related values</li>
  </ul>
  <p>So, for example, in the first row (index=0), there are two observations, at id=5 (x=a, y=0) and at id=9 (x=a, y=1)</p>
  <p>I want to reshape / pivot the data such that there is a column for every location with the corresponding x, y values (can be NA).</p>
  <p>In this example, that would look like this</p>
  <p><a href="https://i.stack.imgur.com/xJxzN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xJxzN.png" alt="enter image description here" /></a></p>
  <p>Here, looking at the first row again, (id=5, x=a, y=0) has translated -&gt; (x_5=a, y_5=0) and (id=9, x=a, y=1) -&gt; (x_9=a, y_9=1). There are no observations for any other locations. So those are <code>&lt;NA&gt;</code></p>
  <p>Have been trying all combinations of <code>.pivot</code> I can think of but can't manage it.</p>
  <p>MRE to produce the example DataFrame's:</p>
  <pre><code>import pandas as pd
  
  df_before = pd.DataFrame(
      [
          [5, 'a', '0', 9, 'a', '1'],
          [5, 'b', '3', 6, 'c', '3'],
          [9, 'c', '4'],
          [2, 'd', '12', 6, 'a', '3', 9, 'c', '2', 4, 'a', '3'],
          [6, 'a', '3'],
          [5, 'b', '0', 9, 'a', '4', 4, 'd', '9'],
          [2, 'c', '2', 9, 'g', '5', 4, 'g', '8']
      ],
      columns=['id_0','x_0','y_0', 'id_1', 'x_1', 'y_1', 'id_2', 'x_2', 'y_2', 'id_3', 'x_3', 'y_3']
  ).fillna(pd.NA)
  
  df_after = pd.DataFrame(
      [
          [pd.NA, pd.NA, pd.NA, pd.NA, 'a', '0', pd.NA, pd.NA, 'a', '1'],
          [pd.NA, pd.NA, pd.NA, pd.NA, 'b', '3', 'c', '3', pd.NA, pd.NA],
          [pd.NA, pd.NA, pd.NA, pd.NA, pd.NA, pd.NA, pd.NA, pd.NA, 'c', '4'],
          ['d', '12', 'a', '3', pd.NA, pd.NA, 'a', '3', 'c', '2'],
          [pd.NA, pd.NA, pd.NA, pd.NA, pd.NA, pd.NA, 'a', '3', pd.NA, pd.NA],
          [pd.NA, pd.NA, 'd', '9', 'b', '0', pd.NA, pd.NA, 'a', '4'],
          ['c', '2', 'g', '8', pd.NA, pd.NA, pd.NA, pd.NA, 'g', '5']
      ],
      columns=['x_2', 'y_2', 'x_4', 'y_4', 'x_5', 'y_5', 'x_6', 'y_6', 'x_9', 'y_9']
  )
  </code></pre>