title: Pivot data and maintain original sort order
body: <p>I'd like to pivot my data which results from a django queryset while maintaining the original (non-alphabetical) sort order on the index column. The pivoted data will then be used in a google visualization line chart.</p>
  
  <p>I've hacked together my own code to do the job but it's a bit ugly and I was wondering if it could be done using a pandas DataFrame pivot.</p>
  
  <p>I've never used pandas before so, after reading the doco, this is what I came up with.</p>
  
  <p>Here is my unpivoted data frame, sorted by date and tenor where tenor suffixes represent: D=Day, M=Month, Y=Year.</p>
  
  <pre><code>df = DataFrame(data)
  
                 date tenor     value
  0        2014-01-01    1D  0.517125
  1        2014-01-01    1M    0.5175
  2        2014-01-01    2M  0.518159
  3        2014-01-01    3M    0.5187
  4        2014-01-01    4M   0.51912
  5        2014-01-01    5M   0.51949
  6        2014-01-01    6M    0.5197
  7        2014-01-01    9M  0.519511
  8        2014-01-01    1Y    0.5198
  9        2014-01-01   18M  0.521228
  10       2014-01-01    2Y  0.523097
  11       2014-01-01    3Y  0.525054
  12       2014-01-01    4Y  0.527055
  13       2014-01-01    5Y  0.529054
  14       2014-01-01    6Y  0.531099
  15       2014-01-01    7Y  0.532852
  16       2014-01-01    8Y  0.534207
  17       2014-01-01    9Y  0.535314
  18       2014-01-02    1D  0.517874
  19       2014-01-02    1M    0.5181
  20       2014-01-02    2M  0.518451
  21       2014-01-02    3M    0.5188
  22       2014-01-02    4M  0.519113
  23       2014-01-02    5M  0.519418
  24       2014-01-02    6M    0.5196
  25       2014-01-02    9M  0.519377
  26       2014-01-02    1Y    0.5197
  27       2014-01-02   18M  0.521406
  28       2014-01-02    2Y  0.523405
  29       2014-01-02    3Y  0.525254
  30       2014-01-02    4Y  0.527151
  31       2014-01-02    5Y  0.529256
  32       2014-01-02    6Y  0.531543
  33       2014-01-02    7Y  0.533457
  34       2014-01-02    8Y  0.534802
  35       2014-01-02    9Y  0.535847
  36       2014-01-03    1D  0.518552
  37       2014-01-03    1M    0.5186
  38       2014-01-03    2M  0.518536
  39       2014-01-03    3M    0.5186
  40       2014-01-03    4M  0.518865
  41       2014-01-03    5M   0.51916
  42       2014-01-03    6M    0.5193
  43       2014-01-03    9M  0.519024
  44       2014-01-03    1Y    0.5193
  45       2014-01-03   18M  0.520882
  46       2014-01-03    2Y    0.5228
  47       2014-01-03    3Y  0.524647
  48       2014-01-03    4Y  0.526752
  49       2014-01-03    5Y  0.528957
  50       2014-01-03    6Y  0.531065
  51       2014-01-03    7Y  0.532856
  52       2014-01-03    8Y  0.534325
  53       2014-01-03    9Y  0.535558
  </code></pre>
  
  <p>Using pandas pivot produces the following results. The pivot worked but the rows are in the wrong order.</p>
  
  <pre><code>df_pivot = df.pivot(index='tenor', columns='date', values='value')
  tenor            2014-01-01 2014-01-02 2014-01-03
  18M                0.521228   0.521406   0.520882
  1D                 0.517125   0.517874   0.518552
  1M                   0.5175     0.5181     0.5186
  1Y                   0.5198     0.5197     0.5193
  2M                 0.518159   0.518451   0.518536
  2Y                 0.523097   0.523405     0.5228
  3M                   0.5187     0.5188     0.5186
  3Y                 0.525054   0.525254   0.524647
  4M                  0.51912   0.519113   0.518865
  4Y                 0.527055   0.527151   0.526752
  5M                  0.51949   0.519418    0.51916
  5Y                 0.529054   0.529256   0.528957
  6M                   0.5197     0.5196     0.5193
  6Y                 0.531099   0.531543   0.531065
  7Y                 0.532852   0.533457   0.532856
  8Y                 0.534207   0.534802   0.534325
  9M                 0.519511   0.519377   0.519024
  9Y                 0.535314   0.535847   0.535558
  </code></pre>
  
  <p>I would like the results sorted by the tenor column:</p>
  
  <pre><code>tenor            2014-01-01 2014-01-02 2014-01-03
  1D                 0.517125   0.517874   0.518552
  1M                   0.5175     0.5181     0.5186
  2M                 0.518159   0.518451   0.518536
  3M                   0.5187     0.5188     0.5186
  4M                  0.51912   0.519113   0.518865
  5M                  0.51949   0.519418    0.51916
  6M                   0.5197     0.5196     0.5193
  9M                 0.519511   0.519377   0.519024
  1Y                   0.5198     0.5197     0.5193
  18M                0.521228   0.521406   0.520882
  2Y                 0.523097   0.523405     0.5228
  3Y                 0.525054   0.525254   0.524647
  4Y                 0.527055   0.527151   0.526752
  5Y                 0.529054   0.529256   0.528957
  6Y                 0.531099   0.531543   0.531065
  7Y                 0.532852   0.533457   0.532856
  8Y                 0.534207   0.534802   0.534325
  9Y                 0.535314   0.535847   0.535558
  </code></pre>
  
  <p>I've thought about writing a custom sort function that would convert the tenor values to days when comparing and then using that with pandas (not sure how yet).</p>
  
  <p>I've investigated using <a href="https://developers.google.com/chart/interactive/docs/querylanguage#Pivot" rel="nofollow">google visualization pivot</a> but that only seems to work on a query not on an existing DataTable.</p>
  
  <p>Any other suggestions would be greatly appreciated.</p>