title: How do I convert one level of pandas MultiIndex column to a standalone column?
body: <p>I have a dataframe with a MultiIndex column with two levels like this:</p>
  <pre><code>import pandas as pd
  df = pd.DataFrame(
      np.arange(16).reshape(4,4),
      columns=pd.MultiIndex.from_tuples(
          ((&quot;ID1&quot;, &quot;Field1&quot;), (&quot;ID1&quot;, &quot;Field2&quot;), (&quot;ID2&quot;, &quot;Field1&quot;), (&quot;ID2&quot;, &quot;Field2&quot;),))
      )
  df.insert(0, &quot;Date&quot;, pd.bdate_range(&quot;2021-11-01&quot;, &quot;2021-11-04&quot;))
  df
  </code></pre>
  <pre><code>Date            ID1             ID2
                  Field1  Field2  Field1  Field2
  0   2021-11-01  0       1       2       3
  1   2021-11-02  4       5       6       7
  2   2021-11-03  8       9       10      11
  3   2021-11-04  12      13      14      15
  </code></pre>
  <p>I am trying to convert level 0 of the MultiIndex column to another column called &quot;ID&quot; so that the dataframe looks like this:</p>
  <pre><code>df1 = pd.DataFrame(
      np.arange(16).reshape(8,2),
      columns=[&quot;Field1&quot;, &quot;Field2&quot;]
  )
  df1.insert(0, &quot;ID&quot;, [&quot;ID1&quot;, &quot;ID2&quot;]*4)
  df1.insert(0, &quot;Date&quot;, pd.bdate_range(&quot;2021-11-01&quot;, &quot;2021-11-04&quot;).repeat(2))
  df1
  </code></pre>
  <pre><code>    Date        ID      Field1  Field2
  0   2021-11-01  ID1     0       1
  1   2021-11-01  ID2     2       3
  2   2021-11-02  ID1     4       5
  3   2021-11-02  ID2     6       7
  4   2021-11-03  ID1     8       9
  5   2021-11-03  ID2     10      11
  6   2021-11-04  ID1     12      13
  7   2021-11-04  ID2     14      15
  </code></pre>
  <p>I tried using the melt function</p>
  <blockquote>
  <p>df.melt(col_level=0, id_vars=[&quot;Date&quot;])</p>
  </blockquote>
  <p>but that combines Field1 and Field2 into a single column instead of leaving them as separate columns as desired.</p>
  <p>Any suggestions would be appreciated. Thank you!</p>