title: Change stacking of dataframe in pandas
body: <p>I have a data frame that looks kind of like this.</p>
  
  <pre><code>Name          2012                  2013                       2014  
           7 8 9 10 11 12   1 2 3 4 5 6 7 8 9 10 11 12  1 2 3 4 5 6 7 8 9 10 11 12
   A       a b c d e f g    a b c d e f g h i j k l m   a b c d e f g h i j k l m
   B       a b c d e f g    a b c d e f g h i j k l m   a b c d e f g h i j k l m
  </code></pre>
  
  <p>and so on. The 2012, 2013, 2014 represents year and below them are its respective months, while a,b,c,d,e... represents the value of the NAME i.e. A,B.. in respective months. The a,b,c,d,e... are different for each name and are displayed here for representation purpose only.</p>
  
  <p>Currently, I have done the following :</p>
  
  <pre><code>workbook = pd.ExcelFile('XYZ.xlsx')
  df = workbook.parse(sheetname='Page1-2')
  df2 = pd.melt(df, id_vars=["Name"], 
                var_name="Date", value_name="Value")
  </code></pre>
  
  <p>i.e I imported my XYZ.xlsx file in df. sorted df to df2 using pd.melt. The output of df2 is something like this:</p>
  
  <pre><code>Name Date      Value
   A   2012      a
   A   Unnamed   b
   A   Unnamed   c
   A   Unnamed   d
   A   Unnamed   e
   A   Unnamed   f
   A   Unnamed   g
   A   2013      a
   A   Unnamed   b
   A   Unnamed   c
   A   Unnamed   d
   A   Unnamed   e
  </code></pre>
  
  <p>and so on with other years and names. I want my date column to come something like this:</p>
  
  <pre><code> Date
   7/2012
   8/2012
   9/2012
   10/2012
   11/2012
   12/2012
   1/2013
   2/2013
   3/2013
   4/2013
   5/2013
   6/2013
   7/2013
   8/2013
  </code></pre>
  
  <p>According to the months and years mentioned in the initial data frame. I'm not sure how to do this. Any help is highly appreciated!     </p>
  
  <p>print (df.to_dict()) of my sample database</p>
  
  <pre><code>{'Name': {0: nan, 1: 'A', 2: 'B'}, 2012: {0: '07', 1: 'a', 2: 'a'},'Unnamed: 2': {0: '08', 1: 'b', 2: 'b'}, 'Unnamed: 3': {0: '09', 1: 'c', 2: 'c'}, 'Unnamed: 4': {0: '10', 1: 'd', 2: 'd'}, 'Unnamed: 5': {0: '11', 1: 'e', 2: 'e'}, 'Unnamed: 6': {0: '12', 1: 'f', 2: 'f'}, '2013': {0: '01', 1: 'a', 2: 'a'}, 'Unnamed: 8': {0: '02', 1: 'b', 2: 'b'}, 'Unnamed: 9': {0: '03', 1: 'c', 2: 'c'}, 'Unnamed: 10': {0: '04', 1: 'd', 2: 'd'}, 'Unnamed: 11': {0: '05', 1: 'e', 2: 'e'}, 'Unnamed: 12': {0: '06', 1: 'f', 2: 'f'}, 'Unnamed: 13': {0: '07', 1: 'a', 2: 'a'}, 'Unnamed: 14': {0: '08', 1: 'b', 2: 'b'}, 'Unnamed: 15': {0: '09', 1: 'c', 2: 'c'}, 'Unnamed: 16': {0: '10', 1: 'd', 2: 'd'}, 'Unnamed: 17': {0: '11', 1: 'e', 2: 'e'}, 'Unnamed: 18': {0: '12', 1: 'f', 2: 'f'}, '2014': {0: '01', 1: 'a', 2: 'a'}, 'Unnamed: 20': {0: '02', 1: 'b', 2: 'b'}, 'Unnamed: 21': {0: '03', 1: 'c', 2: 'c'}, 'Unnamed: 22': {0: '04', 1: 'd', 2: 'd'}, 'Unnamed: 23': {0: '05', 1: 'e', 2: 'e'}, 'Unnamed: 24': {0: '06', 1: 'f', 2: 'f'}, 'Unnamed: 25': {0: '07', 1: 'a', 2: 'a'}, 'Unnamed: 26': {0: '08', 1: 'b', 2: 'b'}, 'Unnamed: 27': {0: '09', 1: 'c', 2: 'c'}, 'Unnamed: 28': {0: '10', 1: 'd', 2: 'd'}, 'Unnamed: 29': {0: '11', 1: 'e', 2: 'e'}, 'Unnamed: 30': {0: '12', 1: 'f', 2: 'f'}} 
  </code></pre>