title: Pandas: How can I move certain columns into rows?
body: <p>Suppose I have the <code>df</code> below. I would like to combine the price columns and value columns so that all prices are in one column and all volumes are in another column. I would also like a third column that identified the price level. For example, <code>unit1</code>, <code>unit2</code> and <code>unit3</code>.</p>
  <pre><code>import numpy as np
  import pandas as pd
  df = pd.DataFrame(
      {
          'uid': ['U100', 'U200', 'E100', 'E200', 'E300', 'A100', 'A200', 'A300', 'A400', 'A500'],
          'location': ['US', 'US', 'EU', 'EU', 'EU', 'Asia', 'Asia', 'Asia', 'Asia', 'Asia'],
          'unit1_price': [10, 20, 15, 10, 10, 10, 20, 20, 25, 25],
          'unit1_vol': [100, 150, 100, 200, 150, 150, 100, 200, 200, 200],
          'unit2_price': [10, 25, 30, 20, 10, 10, 10, 10, 20, 20],
          'unit2_vol': [200, 200, 150, 300, 300, 200, 150, 225, 225, 250],
          'unit3_price': [0, 0, 0, 20, 20, 20, 20, 20, 20, 20],
          'unit3_vol': [0, 0, 0, 500, 500, 500, 500, 500, 500, 500]
      }
  )
  df
  </code></pre>
  <p><a href="https://i.stack.imgur.com/tBfLm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tBfLm.png" alt="df1" /></a></p>
  <p>This is what the final <code>df</code> should look like:</p>
  <p><a href="https://i.stack.imgur.com/GPMfv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GPMfv.png" alt="df_final" /></a></p>
  <hr />
  <p>I tried using <code>melt</code> and I think almost have the right answer.</p>
  <pre><code>pd.melt(df, id_vars=['uid', 'location'], value_vars=['unit1_price', 'unit1_vol', 'unit2_price', 'unit2_vol', 'unit3_price', 'unit3_vol'])
  </code></pre>
  <p>This is what the partial df looks like with <code>melt</code>:</p>
  <p><a href="https://i.stack.imgur.com/W0Ctj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/W0Ctj.png" alt="df2" /></a></p>
  <p>The problem with the above is that volume and price are in the same column but I want them to be in 2 separate columns.</p>
  <p>Did I use the right function?</p>